templates with virutal functions

S

sudhanshu.arora

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
 
V

Vikram

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

Not sure I understand this..why do they 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.

Why not use a vector of base pointers rather than objects directly?
MyVector<Base*> some_vector;
Of course, then you will have to delete the objects in the vector in
your destructor.
 
S

sudhanshu.arora

Both derived_vector and base_vector are pointing to the same vector
which means technically you have only one member variable
MyVector<Base>* base_vector and there are getter functions in base and
derived which can get you MyVector<Base>* and MyVector<Derived>*

Even if I start using MyVector<Base*> instead of storing object values
in vector I won't get what I want.
The requirement here is to stop insertion of base* (or base object)
into an object of type Y.

In general, the essence is
I want to restrict the type of my variables in the derived classes. And
I am not able to achieve this with template classes.

Thanks,
Sudhanshu
 
M

Michiel.Salters

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.

No, they don't. MyVector<Derived> is not derived from MyVector<Base>.
Therefore X::base_vector cannot point to a MyVector said:
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.

In that case, Y should not derive from X. Because the design of X says
that you can add a Base object, and derivation tells you that Y
supports
any operation that X supports. Y does not support all operations,
apparently,
so it shouldn't derive from X. It *may* have a private X member of
course.
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.

Good - the C++ language protects you from implementing a design error.

Michiel.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,433
Messages
2,571,683
Members
48,796
Latest member
Greg L.

Latest Threads

Top