Possible Duplicate:
How can I tell if an object is statically or dynamically allocated on the constructor?
struct Foo {
Foo ();
};
int main ()
{
Foo foo; // Case A
Foo * p_foo = new Foo (); // Case B
}
Foo :: Foo ()
{
if (allocated_on_stack) {
// Case A
}
if (allocated_on_heap) {
// Case B
}
}
Can Foo’s constructor distinguish these two cases?
The short answer to the question is no.
There are theoretical solutions, for instance you could replace
new(on class or global level) and keep track of allnewallocated pointers and compare to this list to yourthispointer in the constructor.(This code won’t catch Foo allocated with
new Foo[count])Why do you need this though?