Hi!
I have a situation (described below) and my lack of expertise has
forced me to fabricate a not so good solution for it. I was thinking if
the experts here can help me.
Situation:
class Base {
int id;
}
class Derived : public Base {
int yid;
}
class X {
MyVector<Base*base_vector;
};
class Y : public X {
MyVector<Derived*derived_vector;
}
Now, in the above scenario, both derived_vector and base_vector point
to the same object and according to the design the base_vector can
contain objects of type base, but if we have Y's object then it
should only contain derived objects. i.e. base_vector is allowed to
contain objects of derived type only.
For this, I was thinking of exposing a virtual getter function in the X
class which returns MyVector<Base>* and overriding it in the derived
class to return MyVector<Derived>*. But since the two template
instantiations are not related to each other, I am not allowed to do
it.
If I do not do this, I am not able to think of a solution which will
provide me compile time check and disallow Base objects to be inserted
into objects of type Y.
Any help is highly appreciated.
Thanks,
Sudhanshu