First off, before I say anything, I just wanted to let you know that you might want to check out these two links for background info on what I'd like to talk about.

Link 1 (note the line that says: "C::f() is not called from B's constructor"):
http://www.devx.com/tips/Tip/13651

Link 2 (this is a question that is similar to myown but doesn't have any solid answer other than "Read the Source Code")
http://www.devx.com/tips/Tip/23751

If you looked into those two links, and you're still with me, you already have my thanks.

Okay... here's what I'm trying to do. On creation of an object, I'd like it to automatically put itself into a hash/manager so the user can never forget or mess up that step... later, on delete, I'd like the object to auto-remove itself from the hash/manager for the same reasons.

Now the twist, the objects are Children and GrandChildren of a Base class.


here's a super minimalist version of what I would like to see happening:
Code:
class Base
{...};

class Child : public Base
{...};

class GrandChild : public Child
{...};

void main( void )
{
  Child *pChild = new Child(); //auto instert self into a hash
  GrandChild *pGrandChild = new GrandChild(); //auto instert self into a hash

  delete pGrandChild; //auto remove self from hash
  delete pChild; //auto remove self from hash
}
naturally I created a virtual function "GetTypeID()" that was define in all three classes and tells the hash where to store the object.

so this worked:
Code:
class Base
{
  //...
  virtual int GetTypeID( void ) { return 1; }
};

class Child : public Base
{
  //...
  virtual int GetTypeID( void ) { return 2; }
};


class GrandChild : public Child
{
  //...
  virtual int GetTypeID( void ) { return 3; }
};


void main( void )
{
  Child *pChild = new Child();
  GrandChild *pGrandChild = new GrandChild(); 

  AddToManager( pChild, pChild->GetTypeID() );
  AddToManager( pGrandChild, pGrandChild->GetTypeID() );


  RemoveFromManager( pGrandChild, pGrandChild->GetTypeID() );
  RemoveFromManager( pChild, pChild->GetTypeID() );

  delete pGrandChild; 
  delete pChild; 
}

however, the moment I moved those "AddToManager()" and "RemoveFromManager()" calls into Base's constructor and deconstructor, it stopped working for reasons that were explained in those to links I gave at the top of this post.

I understand why it didn't work. I'm cool with that. This post is not to ask someone to explain it to me. This post is to ask "knowing all this, where do I go from here?"

again, I'd just like Child and GrandChild to add and remove themselves from a hash/manager automatically. But I'm stuck because every solution I do get to work ends up being very messy and involves Every Inherited Class Redefining a New and Delete function.

It just seems to me that Objects that handle themselves in this manner must occur all the time since C++ is centered on Object Orented coding. I feel like, for an OO Langauge, the code should be way easier than what I've been doing, therefore I figure I must be doing something wrong.

For those who are still with me (and I thank you), here is my working code. Is there a way to make it more elegent?

Thanks Everyone,
~Danny B.

My Messy Working Code, Followed by the output that it generates:
Code:
#include "stdio.h"


class Base
{
protected:
	Base(){ printf( "Base()\n" ); }
	~Base(){ printf( "~Base()\n" ); }
public:

	static Base* New( void )
	{
		Base *pB = new Base();
		printf( "AddToSuperCoolManager_ByTypeID( %i );\n", pB->GetTypeID() );
		return pB;
	}
	
	virtual void DeleteSelf( void )
	{
		printf( "RemoveFromSuperCoolManager_ByTypeID( %i );\n", this->GetTypeID() );
		delete this;
	}

	virtual int GetTypeID( void ) { return 1; }

}; // end class Base -----------------


class Child : public Base
{
protected:
	Child(){ printf( "Child()\n" ); }
	~Child(){ printf( "~Child()\n" ); }
public:

	static Child* New( void )
	{
		Child *pC = new Child();
		printf( "AddToSuperCoolManager_ByTypeID( %i );\n", pC->GetTypeID() );
		return pC;
	}

	virtual void DeleteSelf( void )
	{
		printf( "RemoveFromSuperCoolManager_ByTypeID( %i );\n", this->GetTypeID() );
		delete this;
	}

	virtual int GetTypeID( void ) { return 2; }

}; // end class Child -----------------


class GrandChild : public Child
{
protected:
	GrandChild(){ printf( "GrandChild()\n" ); }
	~GrandChild(){ printf( "~GrandChild()\n" ); }
public:

	static GrandChild* New( void )
	{
		GrandChild *pGC = new GrandChild();
		printf( "AddToSuperCoolManager_ByTypeID( %i );\n", pGC->GetTypeID() );
		return pGC;
	}

	virtual void DeleteSelf( void )
	{
		printf( "RemoveFromSuperCoolManager_ByTypeID( %i );\n", this->GetTypeID() );
		delete this;
	}

	virtual int GetTypeID( void ) { return 3; }

}; // end class GrandChild -----------------


void main( void )
{
	Child *pChild = Child::New();
	GrandChild *pGrandChild = GrandChild::New();

	pGrandChild->DeleteSelf();
	pChild->DeleteSelf();
}

Output from above code:
Code:
Base()
Child()
AddToSuperCoolManager_ByTypeID( 2 );
Base()
Child()
GrandChild()
AddToSuperCoolManager_ByTypeID( 3 );
RemoveFromSuperCoolManager_ByTypeID( 3 );
~GrandChild()
~Child()
~Base()
RemoveFromSuperCoolManager_ByTypeID( 2 );
~Child()
~Base()