473,386 Members | 2,078 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

templates with virutal functions

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

Jul 17 '06 #1
4 1320
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.

Jul 17 '06 #2
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

Vikram wrote:
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.
Jul 18 '06 #3
su*************@gmail.com wrote:
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<Derivedis not derived from MyVector<Base>.
Therefore X::base_vector cannot point to a MyVector<Derivedobject.
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.

Jul 18 '06 #4
su*************@gmail.com wrote:
Both derived_vector and base_vector are pointing to the same vector

Please don't top-post. Your reply belongs following or interspersed
with properly trimmed quotes. See the newsgroup FAQ:
<http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4>


Brian
Jul 18 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Senthilraja | last post by:
I have the following program using templates. Someone please let me know the syntax to be used for defining the member functions push, pop etc. as non-inline functions. #include <iostream>...
5
by: Ray Gardener | last post by:
Would templates be necessary if C++ had all numeric types inherit from a base "number" class and had all types inherit from a base "object" class? (Sorry if this has already been discussed to...
2
by: kelvSYC | last post by:
I'm trying to program something along the lines of a "trading card" idea: I have a single copy of the "card" in the program, yet there may be multiple "instances" of the "card", with differing...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
11
by: Elpoca | last post by:
Hi: What rules govern the inlining of templated functions and templated class methods? It has always been my understanding that both templated functions and templated class methods were...
5
by: Eric Fortier | last post by:
Hi all, Last year I posted a message regarding templates use in blitter functions, but the single answer I got didn't help. My problem is that I was writing blitter functions which takes a...
28
by: NewToCPP | last post by:
Hi, I am just trying to find out if there is any strong reason for not using Templates. When we use Templates it is going to replicate the code for different data types, thus increasing the...
3
by: Steven T. Hatton | last post by:
Has anybody here used explicit instantiation of templates? Has it worked well? Are there any issues to be aware of? -- NOUN:1. Money or property bequeathed to another by will. 2. Something...
104
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.