I have some unusual requirement that a variable should always be there on heap and not on stack. Now I tried doing this using private destructor and static method of class which will simply take pointer and call delete on it.
Class A
{
public :
static void method(const A* ptr)
{
delete ptr;
}
private :
~A();
};
But now I am just curious to see better altrnative and one thing came to my mind is if I can add some pre-check to each method to see wheather variable is on stack or on heap then I do not have to declare static method. Can anyone tell me how to do that? I just got one solution on my mind is to use sbrk(0) or pthread API to get boundary of current stack and then compare it with address of class variable but not portable.
Thanks Niraj Rathi