473,372 Members | 997 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,372 software developers and data experts.

container of pointers and virtual member function templates

Sat
Hi,

I have a simplified version of a problem that I am facing (hope I haven't
oversimplified it). This code doesn't work now and I want to find how I can
make it work. Can I call the derived class version from the base class
pointer and still be able to use a vector with derived element pointers?

class BaseElem { };

class DerivedElem1 { };

class DerivedElem2 { };

class BaseContainer
{
public:
virtual void GetAllElems(std::vector<BaseElem*>& allElems) const;
};

class DerivedContainer1
{
public:
virtual void GetAllElems(std::vector<DerivedElem1*>& allElems) const;
};

class DerivedContainer2
{
public:
virtual void GetAllElems(std::vector<DerivedElem2*>& allElems) const;
};
Jul 19 '05 #1
4 4623
"Sat" <re******************@hotmail.com> wrote in message
news:bo**********@news01.intel.com...
Hi,

I have a simplified version of a problem that I am facing (hope I haven't
oversimplified it). This code doesn't work now and I want to find how I can make it work. Can I call the derived class version from the base class
pointer and still be able to use a vector with derived element pointers?

class BaseElem { };

class DerivedElem1 { };

class DerivedElem2 { };
You haven't derived either of the last two classes from the first one.
class BaseContainer
{
public:
virtual void GetAllElems(std::vector<BaseElem*>& allElems) const;
};

class DerivedContainer1
{
public:
virtual void GetAllElems(std::vector<DerivedElem1*>& allElems) const;
};

class DerivedContainer2
{
public:
virtual void GetAllElems(std::vector<DerivedElem2*>& allElems) const;
};


Again, neither of your "derived" containers derives from your base
container. In fact, you have no inheritance anywhere in this code.

Even if you specify the inheritance that you appear to have intended for
both elements and containers, your derived-class virtual functions do not
override the base-class virtual function. A vector<BaseElem*> is a type
unrelated to vector<DerivedElem1*>, so if I've understood your question
correctly the answer is "no".

DW

Jul 19 '05 #2
Sat
Thanks David, You understood the question correctly. The inheritance part
was missing in my code.

I have re-written it here:
class BaseElem { };

class DerivedElem1 : public BaseElem { };

class DerivedElem2 : public BaseElem { };

class BaseContainer
{
public:
virtual void GetAllElems(std::vector<BaseElem*>& allElems) const;
};

class DerivedContainer1 : public BaseContainer
{
public:
virtual void GetAllElems(std::vector<DerivedElem1*>& allElems) const;
};

class DerivedContainer2 : public BaseContainer
{
public:
virtual void GetAllElems(std::vector<DerivedElem2*>& allElems) const;
};

I understand that the std::vector<DerivedElem*> is unrelated to
std::vector<BaseElem*>. I am trying to see if anyone has a good idea of
implementing this in a different way. The intent is to return a bunch of
DerivedElem pointers in a container using a virtual function.

I tried making GetAllElems a templated member function like this:

template<ElemType>
virtual void GetAllElems(std::vector<ElemType>& allElems) const;

but later found that templated member functions cannot be virtual functions.
That's why I am stuck.

Thanks
Sat

"David White" <no@email.provided> wrote in message
news:Go*****************@nasal.pacific.net.au...
"Sat" <re******************@hotmail.com> wrote in message
news:bo**********@news01.intel.com...
Hi,

I have a simplified version of a problem that I am facing (hope I haven't oversimplified it). This code doesn't work now and I want to find how I

can
make it work. Can I call the derived class version from the base class
pointer and still be able to use a vector with derived element pointers?

class BaseElem { };

class DerivedElem1 { };

class DerivedElem2 { };


You haven't derived either of the last two classes from the first one.
class BaseContainer
{
public:
virtual void GetAllElems(std::vector<BaseElem*>& allElems) const;
};

class DerivedContainer1
{
public:
virtual void GetAllElems(std::vector<DerivedElem1*>& allElems) const; };

class DerivedContainer2
{
public:
virtual void GetAllElems(std::vector<DerivedElem2*>& allElems) const; };


Again, neither of your "derived" containers derives from your base
container. In fact, you have no inheritance anywhere in this code.

Even if you specify the inheritance that you appear to have intended for
both elements and containers, your derived-class virtual functions do not
override the base-class virtual function. A vector<BaseElem*> is a type
unrelated to vector<DerivedElem1*>, so if I've understood your question
correctly the answer is "no".

DW

Jul 19 '05 #3
"Sat" <re******************@hotmail.com> wrote in message
news:bp**********@news01.intel.com...
I understand that the std::vector<DerivedElem*> is unrelated to
std::vector<BaseElem*>. I am trying to see if anyone has a good idea of
implementing this in a different way. The intent is to return a bunch of
DerivedElem pointers in a container using a virtual function.

I tried making GetAllElems a templated member function like this:

template<ElemType>
virtual void GetAllElems(std::vector<ElemType>& allElems) const;

but later found that templated member functions cannot be virtual functions.

That's right, probably because of implementation difficulties for the
compiler in constructing a v-table that can support templates.
That's why I am stuck.


I can't think of any really nice way to do it. The best I can come up with
is a virtual function that takes a std::vector<BaseElem*> &, and a
non-virtual member function template in the base class that calls the
virtual function and converts the vector it creates into another vector of
the correct type of element using a cast. Or, if you want to ensure type
safety at the expense of execution time, you can dynamic_cast every element
separately.

Another, even nastier, method - but fast - would be to directly cast the
std::vector<DerivedElem*> & passed to the template member in the base class
into a std::vector<BaseElem*> & for passing to the virtual function.

I probably need a few slaps across the face from other regulars here or I
might come up with something even worse :-)

DW

Jul 19 '05 #4
"David White" <no@email.provided> wrote in message
news:hz*****************@nasal.pacific.net.au...

I can't think of any really nice way to do it. The best I can come up with
is a virtual function that takes a std::vector<BaseElem*> &, and a
non-virtual member function template in the base class that calls the
virtual function and converts the vector it creates into another vector of
the correct type of element using a cast.


To clarify, here I meant casting the address of the first element of
std::vector<BaseElem*>, which is a BaseElem**, into a DerivedElem** for
insertion into the std::vector<DerivedElem*> & passed to the template
function. I didn't mean casting one vector type into another, as it
appeared. (Though I meant exactly that later on.)

DW

Jul 19 '05 #5

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

Similar topics

6
by: Johan Bergman | last post by:
Hi, Maybe someone can help me with this one. The following describes a somewhat simplified version of my problem, but I think it will be sufficient. I want to use class factories (virtual...
4
by: Michael | last post by:
Ok, I understand the function pointers in C, but i'm a bit more confused in c++. Firstly, as i understand it, a static member function can be "function-pointed" to as a normal c function, but in...
3
by: Ed | last post by:
Hi, I have a WorkUnit class. I will pass a reference to a group of these WorkUnits to other classes in my application. I have chosen to use a vector to hold pointers to these WorkUnits for now....
5
by: Martijn van Buul | last post by:
Hi. I'm having a peculiar problem at work. I've been googling for it, but haven't found an authorative answer. In a nutshell (Long story follows), what I'd like to know is: If I have a C++...
3
by: Ernesto Bascón | last post by:
Hi everybody: I have two questions: 1. I'm using opaque pointers in my classes to hide their data structures; there is a way to use opaque pointers in template classes; since the...
18
by: tbringley | last post by:
I am a c++ newbie, so please excuse the ignorance of this question. I am interested in a way of having a class call a general member function of another class. Specifically, I am trying to...
7
by: ademirzanetti | last post by:
Hi there !!! I would like to listen your opinions about inherit from a STL class like list. For example, do you think it is a good approach if I inherit from list to create something like...
4
by: Bit Byter | last post by:
I want to write a (singleton) container for instances of my class templates, however, I am not too sure on how to: 1). Store the instances 2). How to write the acccesor method (instance()) to...
5
by: want.to.be.professer | last post by:
For OO design, I like using virtual member function.But considering efficiency, template is better. Look at this program, class Animal { public: virtual void Walk() = 0; }; class Dog
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.