Skip to main content
gamedev.net
🔒 Locked

Overloading class operator new

Started by kspansel 4 replies 400+ views
Original Post
kspansel
kspansel
If I had a base class that had it''s new operator overloaded, would it be possible to access that object? Example class Object { void * operator new( size_t size ) { void * pNewObj = malloc( size ); Object *pObj = (Object *)pNewObj; pObj->value = someValue; return pNewObj; } }; I know that the constructor has not yet been called, but when it does will the value be set to what I want? What I want to do is to allow my object to store it''s list index when it is placed into the list, which happens to occur in the overloaded new operator. Any ideas?? Kory
"The pioneers of a warless world are the youth who refuse military service" - Albert Einstein
patindahat
patindahat
First off, where are you going to get this value? because from the code you showed it looked like you''d need to add another operator to the new operator.

I can''t answer you specific question as I''ve never done it... Its its easy enough to check... In your constructor before all else send a message box showing the value of the member you set. Or output it to the console, or a file or whatever.

Pat
original vesoljc
original vesoljc
something like this?


// declare

static void* operator new (size_t);
static void operator delete (void*, size_t);

// define

void* CLASS::operator new (size_t lSize)
{
void* lRetPtr = 0;
try
{
lRetPtr = ::operator new(lSize);
}
catch (exception* lExcp)
{
throw;
}
return(lRetPtr);
}

void CLASS::operator delete(void* lPtr, size_t lSize)
{
::operator delete(lPtr);
}


as for setting that value, do that in the constructor...


one more thing:
the above code is giving a warning:
f:\axpbuild15\main.cpp(31) : warning C4291: ''void *__cdecl axp::axpAMemManager::operator new(unsigned int)'' : no matching operator delete found; memory will not be freed if initialization throws an exception

any ideas why?
Abnormal behavior of abnormal brain makes me normal...
dalleboy
dalleboy
quote:
Original post by kspansel
If I had a base class that had it''s new operator overloaded, would it be possible to access that object?

Short answer: No.

Long answer: Sure you could fiddle with the allocated memory, but anything you do to the object will be overwritten when the constructor for the object is executed, and thus it is useless.

quote:
Original post by kspansel
What I want to do is to allow my object to store it''s list index when it is placed into the list, which happens to occur in the overloaded new operator.

Do it in the constructor instead of in the operator new.

[How To Ask Questions|C++ library of container classes, algorithms and iterators. It provides many of the basic algorithms and data structures of computer science." target="_blank">STL Programmer''s Guide|C++ Style and Technique FAQ - Designer and implementor the C++ programming language." target="_blank">Bjarne FAQ|C++ FAQ Lite - Frequently Asked Questions about the C++ language." target="_blank">C++ FAQ Lite|C++ Reference - The standard C++ library is a collection of functions, constants, classes and objects that extends the C++ language providing basic functionality to interact with the operating system and some standard classes, objects and algorithms that may be commonly needed." target="_blank">C++ Reference|MSDN]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.