简体   繁体   中英

c++ - overloading operator new

I'am a little bit confused about operator overloading new and delete. I wrote some test :

#include <iostream>

using namespace std;

class Test
{
    public :

    Test()
    {
        cout << "Test - ctor" <<endl;
    }

    ~Test()
    {
        cout << "Test - dtor" <<endl;
    }

    Test(const Test& t)
    {
        cout << "Test - copy ctor" <<endl;
    }

    Test& operator = (const Test& t)
    {
        cout << "Test - assiment operator" <<endl;
    }

    void* operator new(size_t size)
    {
        cout << "Test - operator new" <<endl;
        return NULL;
    }

    void print()
    {
        cout << "print" << endl;
    }
};


int main()
{
   Test* t = new Test();
   t->print();
   return 0;
}

And the output is :

Test - operator new                                                                                                                                                                                
Test - ctor                                                                                                                                                                                        
print  

Now, if I return "NULL" from "new" , why my program doesn't crash when I call to print function? thanks.

Because print() doesn't actually need anything of it's class Test . All it does is print a message to stdout . Remember that t->print(); is the same as print(t); , your function signature is actually:

void print(Test* t);

But that's all done by the compiler for you.

t just isn't used in the method and thus you're (un)lucky because it runs. This is still just plain undefined behaviour though.

If you absolutely want to see things crash and burn then change your class a bit:

class Test
{
    public :

    Test() : x(0)
    {
        cout << "Test - ctor" <<endl;
    }

    ~Test()
    {
        cout << "Test - dtor" <<endl;
    }

    Test(const Test& t)
    {
        cout << "Test - copy ctor" <<endl;
    }

    Test& operator = (const Test& t)
    {
        cout << "Test - assiment operator" <<endl;
    }

    void* operator new(size_t size)
    {
        cout << "Test - operator new" <<endl;
        return NULL;
    }

    void print()
    {
        cout << "print" << x << endl;
    }

private:
    int x;
};

This specific phenomenon has nothing to do with your operator new function.

The behaviour on calling a member function through a NULL pointer is undefined . Your result is a manifestation of that undefined behaviour.

It just happens to work in your particular case (possibly because your class is just a bag of functions).

(These days it's a good idea to use nullptr rather than the old-fashioned and not absolutely correct NULL .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM