简体   繁体   中英

How to understand new operator overloading?

For "+" operator overloading, it is pretty easy to understand. c = c1.operator+(c2) is function notation, c = c1 + c2 is operator notation.
However, I just couldn't understand new operator overloading. In the following code:
Please tell me what happened when processing student * p = new student("Yash", 24); Why void * operator new(size_t size) is called. And why size is 28 when entering operator new(size_t size) .

// CPP program to demonstrate 
// Overloading new and delete operator 
// for a specific class 
#include<iostream> 
#include<stdlib.h> 

using namespace std; 
class student 
{ 
    string name; 
    int age; 
public: 
    student() 
    { 
        cout<< "Constructor is called\n" ; 
    } 
    student(string name, int age) 
    { 
        this->name = name; 
        this->age = age; 
    } 
    void display() 
    { 
        cout<< "Name:" << name << endl; 
        cout<< "Age:" << age << endl; 
    } 
    void * operator new(size_t size) 
    { 
        cout<< "Overloading new operator with size: " << size << endl; 
        void * p = ::new student(); 
        //void * p = malloc(size); will also work fine 
    
        return p; 
    } 

    void operator delete(void * p) 
    { 
        cout<< "Overloading delete operator " << endl; 
        free(p); 
    } 
}; 

int main() 
{ 
    student * p = new student("Yash", 24); 

    p->display(); 
    delete p; 
} 

The arguments in

student * p = new student("Yash", 24);

are arguments to the student constructor, they are not passed to operator new whose only responsibility is to allocate enough memory for the student object.

In order to allocate enough memory for a student object operator new has to be told how much memory it needs to allocate, This is what 28 is, it's the value of sizeof(student) .

So your code

void * operator new(size_t size) 
{ 
    cout<< "Overloading new operator with size: " << size << endl; 
    void * p = ::new student(); 
    //void * p = malloc(size); will also work fine 

    return p; 
} 

is actually incorrect because you are creating a student object which is not the responsibility of operator new . The commented out code that uses malloc is correct however.

To see this problem you should add the following

student(string name, int age) 
{ 
    cout<< "Constructor is called with " << name << " and " << age "\n" ; 
    this->name = name; 
    this->age = age; 
} 

Now you will see that your bugged version calls two constructors for one object. This is because you are wrongly calling the constructor from operator new .

It is possible to explicitly pass arbitrary values from the user code to operator new . If you want to investigate this then look up placement new .

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