On Apr 28, 6:05 pm, Rob <someidunknown1...@[EMAIL PROTECTED]
> wrote:
> I have these cl***** (elided methods):
>
> class Base
> {
> public:
> Base(string name) {...}
>
> };
>
> class Derived : public Base
> {
> public:
> Derived(String name) : Base( name ) {...}
>
> };
>
> And neither of these work:
> snip....
std::vector<Derived*> and std::vector<Base*> are completely different
types, and unrelated. YOu may as well have said std::vector<Foo*>and
std::vector<Bar*> .
Something similar does work in Java (and that is a recent innovation
in itself -- for the longest time all collections in Java only held
Object and you had to cast things about to get it to work...), but not
C++.
The answer in C++ is to make create a template function, i.e.
template<class T>void create(T&v, int mytype){
if(mytype==0)v.push_back(new Derived);
else v.push_back(new Derived2);//something else derived from Base
}
Lance
--
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|