I have a base class with 2 derived classes and another one which store objects of the derived classes type:
class X
{};
class X1 : public X
{};
class X2 : public X
{};
class Y
{
std::vector<X*> V;
};
why doesn't this method of introducing elements in V work?
X1 object;
X *ptr = &object;
V.push_back(ptr);
Vwill be invalid as soon ` object` goes out of scope.Vis a member of the classY. And since this cannot be a member function (becauseYdoesn't have any), andVis private, there cannot be an unqualified reference toVlike this.