class Base
{
public:
Base (int a, int b);
private:
int a,b;
};
class Derived1
{
public:
Derived1():base(1,2){}
};
similarly Derived2, Derived 3 which doesnt contain any data members on its own
Now i need to contain these derived objects in a singleton, so i was thinking to call this in base constructor like
Base::Base(int a, int b)
{
CBaseMgr::GetInstance()->AddtoVector(this);
}
so now if i construct
Derived d1, d2, d3 etc. will the Singleton’s container contain all derived objects?
My doubt is can i do this adding of objects to container in base ctor or should i do in derived ctor.?
If all the derived class call this base class constructor, yes, you should be fine.
Just beware of the copy constructor which, if not overloaded, will not add
thisto your global vector.I suppose you want as well remove the instances that were destroyed, from the global vector ?
If so, don’t forget to declare
Base::~Baseto bevirtual, so that he gets called by derived classes.