Jump to content

Question about c++ new overriding

P1X3

Let's say I have a class called "Object" and it overrides "new" operator which still allocates memory but returns a reference rather than a pointer. The override method also keeps track of the pointer.

If I create a class "Monster" whose parent is "Object", does "new" operator override affect child class? If not, is it possible to override "Monster"'s new operator as well without specifying it explicitly?

 

 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Ok, so Monster should inherit all the overriden functions from object.  So yes the "new" operator does override the child class.  Take this quick example:

class testobj {public:	void* operator new(std::size_t size) {		return (void*)0;	}public:	char* internalText;	int num;};class testobj2: public testobj {};class testobj3: public testobj {};int _tmain(int argc, _TCHAR* argv[]){	testobj* test = new testobj();	testobj2* test2 = new testobj2();	testobj* test3 = new testobj3();	std::cout << test <<'\n';	std::cout << test2 <<'\n';	std::cout << test3 <<'\n';	char wait;	std::cin >> wait;	return 0;}

The output is 0, 0, 0 so the objects took on their parent's new operator

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Ok, so Monster should inherit all the overriden functions from object.  So yes the "new" operator does override the child class.  Take this quick example:

class testobj {public:	void* operator new(std::size_t size) {		return (void*)0;	}public:	char* internalText;	int num;};class testobj2: public testobj {};class testobj3: public testobj {};int _tmain(int argc, _TCHAR* argv[]){	testobj* test = new testobj();	testobj2* test2 = new testobj2();	testobj* test3 = new testobj3();	std::cout << test <<'\n';	std::cout << test2 <<'\n';	std::cout << test3 <<'\n';	char wait;	std::cin >> wait;	return 0;}

The output is 0, 0, 0 so the objects took on their parent's new operator

 

Interesting.

 

Another somewhat weird question. I create an instance of testobj2.

testobj2 to2 = new testobj2(); // assuming new override returns reference, not a pointer

Does deconstructor gets called when the variable that holds the reference to the object gets out of scope?

 

I am asking this because I was thinking about writing a base class, similar to Object in java. The base class would override new operator to handle memory allocation and then it will keep track of how many variables are pointing to it. When there is one variable pointing to it which then gets deleted or goes out of scope, the base class will delete the object. Kinda silly, but ehh.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Interesting.

 

Another somewhat weird question. I create an instance of testobj2.

testobj2 to2 = new testobj2(); // assuming new override returns reference, not a pointer

Does deconstructor gets called when the variable that holds the reference to the object gets out of scope?

 

I am asking this because I was thinking about writing a base class, similar to Object in java. The base class would override new operator to handle memory allocation and then it will keep track of how many variables are pointing to it. When there is one variable pointing to it which then gets deleted or goes out of scope, the base class will delete the object. Kinda silly, but ehh.

 

This line of code would create a new instance of testobj2.  Since your variable "to2" is not declared as a pointer or reference (e.g. testobj2& or testobj2*), you are actually performing the equals operator. Depending on the rest of your code, this is likely causing a memory leak.

Link to comment
Share on other sites

Link to post
Share on other sites

Never really tried, but I can tell you my guess.

 

Guess:

When overloading the new operator things usually handled by the base class aren't called, such as a default constructor call or the general initialization of the object.

As the new operator is still passing a reference, the variable locally will be a pointer.  The problem with this is delete will still have to be called inside the function, otherwise the pointer's destructor is called not the object's destructor.

 

Just spit balling this but perhaps the better case would be to create a class which just monitors the pointers.  For example (just wrote this up quickly without compiling or anything so excuse any mistakes)

class PointerCount {public:	PointerCount(void* pointer): pPointer(pointer) {		count = (int*)malloc(sizeof(int));		*count = 1;	}	//Not threadsafe	~PointerCount() {		if(*count == 1) {			delete *pPointer; //Not sure if this would work, but easy enough to get it to work like this			free count;		}	}	PointerCount(const PointerCount& other) {		(*other.count)++;		pPointer = other.pPointer;		count = other.count;	}	void* pPointer;private:	int* count;};

The concept of this is you have a variable defined in stack so PointerCount will always get it's destructor called (assuming you don't use new, but just PointerCount(new testObj()) for eg).  You can always recover your object via PointerCount.pPointer, bit dangerous but you could put safeguards with templates in place or make it look more pretty, but this is just an example

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×