Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Question about "// ::operator delete(pt);"

24 views
Skip to first unread message

fl

unread,
Oct 2, 2015, 2:16:25 PM10/2/15
to
Hi,

I see a code snippet online, see below please. I don't understand the
comments after 'delete pt;'. If I run the code, message "MyClass destroyed"
is printed out. In stead, if I run:

::operator delete(pt);

the above message does not print out. Then, my question is why there is such
a difference. '::operator delete(pt);' is from C++ standard library?


Thanks for help.




///////////////
#include <iostream> // std::cout

struct MyClass {
MyClass() {std::cout <<"MyClass constructed\n";}
~MyClass() {std::cout <<"MyClass destroyed\n";}
};

int main () {
MyClass * pt = new (std::nothrow) MyClass;
delete pt; // implicitly calls ::operator delete(pt)

return 0;
}

Bo Persson

unread,
Oct 2, 2015, 2:37:48 PM10/2/15
to
Yes, "delete pt" does more than just calling operator delete. I first
also calls the destructor for your object.


Bo Persson

0 new messages