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