C++ Add this pointer to a container by calling it in base class constructor
- by vivekeviv
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.?