473,625 Members | 2,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<BaseEl em*>& allElems) const;
};

class DerivedContaine r1
{
public:
virtual void GetAllElems(std ::vector<Derive dElem1*>& allElems) const;
};

class DerivedContaine r2
{
public:
virtual void GetAllElems(std ::vector<Derive dElem2*>& allElems) const;
};
Jul 19 '05 #1
4 4639
"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<BaseEl em*>& allElems) const;
};

class DerivedContaine r1
{
public:
virtual void GetAllElems(std ::vector<Derive dElem1*>& allElems) const;
};

class DerivedContaine r2
{
public:
virtual void GetAllElems(std ::vector<Derive dElem2*>& 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<DerivedE lem1*>, 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<BaseEl em*>& allElems) const;
};

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

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

I understand that the std::vector<Der ivedElem*> is unrelated to
std::vector<Bas eElem*>. 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<ElemTy pe>
virtual void GetAllElems(std ::vector<ElemTy pe>& 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.provi ded> 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<BaseEl em*>& allElems) const;
};

class DerivedContaine r1
{
public:
virtual void GetAllElems(std ::vector<Derive dElem1*>& allElems) const; };

class DerivedContaine r2
{
public:
virtual void GetAllElems(std ::vector<Derive dElem2*>& 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<DerivedE lem1*>, 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<Der ivedElem*> is unrelated to
std::vector<Bas eElem*>. 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<ElemTy pe>
virtual void GetAllElems(std ::vector<ElemTy pe>& 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<Bas eElem*> &, 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<Der ivedElem*> & passed to the template member in the base class
into a std::vector<Bas eElem*> & 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.provi ded> 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<Bas eElem*> &, 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<Bas eElem*>, which is a BaseElem**, into a DerivedElem** for
insertion into the std::vector<Der ivedElem*> & 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
2045
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 constructors) which have this base class: template<class T>
4
387
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 order to pointer to f2, an object must be instatiated. Is this correct?? class cl { public:
3
1669
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. However, I think that I should hide the fact that it's a vector in case I want to switch to another container class at a later date. I don't want every class that needs to use the group of WorkUnits to be too tightly bound to the details of the...
5
3174
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++ class which has several virtual functions and several member variables, will the default copy constructor also copy the virtual table pointer, or will it merely do a (shallow) copy of the member variables? I'm using gcc 4.1.1
3
2159
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 implementation and the declaration of the template class is in the same file, the use of opaque pointers does not make sense, but... what about implementation hiding for libraries? how can I make sure that the new versions of my libraries have binary...
18
2859
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 write an ordinary differential equation class that would solve a general equation in the form: dx/dt = f(x,t). The ode class shouldn't know anything about f, except how to call it.
7
3121
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 "myList" as in the example below ? #include "Sector.h" using namespace boost;
4
2364
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 retrieve an instance of particular template 3). What type to return an instance as .. Assuming I have the following code:
5
2952
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
0
8253
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8189
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8354
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6116
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4192
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1499
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.