New operator overloading issue

Hi,
I'm trying to achieve something like this:

I have struct Node.

1
2
3
4
struct Node{
Node* parent; //null if it is root
Node* children; //0 - x
};

Futher I have ManagedObject class.

1
2
3
4
5
6
7
8
9
10
11
class ManagedObject{
private:
static Node* roots;

public:
virtual void* operator new(size_t size){
//And here is problem. I need:
//a)If new is called for "main object" place new node into roots (parent == null)
//else b) if new is called for "member scope object" look for "main object"'s root node and set this node as its children.
}
};





My explanation is maybe chaotic so I show you example:

1
2
3
4
5
6
7
8
9
class A : public ManagedObject{
private:
ManagedObject* nestedObject;

public:
A(){
nestedObject = new ManagedObject;
}
};

in another part of program:

...
ManagedObject* some = new A;
...

So new operator should place root node for "some" and simultaneously it should create node for "nestedObject" and set it as child of previous root node.




Is it possible?


Thank you
Last edited on
Generally, you would just use some sort of smart pointer (like the STL's or Boost's) rather then inheriting from a base class.
I am trying to implement my GC (http://iwi.eldoc.ub.rug.nl/root/2007/SciCompProgGao/) for ManagedObject.
However, I'm still thinking over how I should implement it.
Last edited on
Topic archived. No new replies allowed.