Overloading Remove operator in C ++

In my code, I overloaded the operators newand deleteto get the file name and line number. In my code I use mapand stack. When I delete a specific value from the map, it just calls my overloaded function delete, but I want only explicit instructions deleteto be able to access my function and not others. How can i do this?

+3
source share
2 answers

If you want to delete it yourself to call overload, I would not overload the operator deletion, but instead of making a custom method like DeleteMyObject, which I would call the initial delete, then create a macro to call this function and replace all your deletions with this macro.

how

#define DELETE_MY_OBJECT(obj) DeleteMyObject(obj, __FILE__, __LINE__)

and the method may look like

void DeleteMyObject(void* obj, char* file, char* line)
{
    // Log delete
    ...

    delete obj;
}

then in your code

MyObj* obj = ...
...
DELETE_MY_OBJECT(obj);
+3
source

mapand stack are , explicitly calling delete yourObjectunder the hood, for most "explicit" definitions, etc., why your delete operator gets called. These deletions are no less legal than those contained in your code.

, , ? , __FILE__ __LINE__, , . , , , delete, .

, , , . delete. :

#define DELETE(p) \
    do { \
        std::cout << __FILE__ << ":" << __LINE__ << ": " << p << std::endl; \
        delete p; \
    }
+1

Source: https://habr.com/ru/post/1763790/


All Articles