Re: Question about design?
- From: "Victor Bazarov" <v.Abazarov@xxxxxxxxxxxx>
- Date: Sat, 18 Mar 2006 23:55:22 -0500
Matt wrote:
I have the following code:
class Base { ... }
template <class T> class Derived : Base { public: T value; ...}
vector<Base*> v;
v.push_back(new Derived<int>(1));
Shouldn't compile. 'Base' is a private base class, conversion to
a pointer to it from Derived<> is not allowed.
v.push_back(new Derived<string>("abc"));
...
To get the value, I had to do the cast
((Derived<int>*) v.at(0))->value = 2;
((Derived<string>*) v.at(1)->value = "xxx";
Any better way to do it? Thanks.
Well, the use of 'dynamic_cast' is preferred to what you tried
here. And if you're going to use 'dynamic_cast', read about the
possible result if the class is not the one to which you try
converting.
V
--
Please remove capital As from my address when replying by mail
.
- Prev by Date: Re: Newbie Question - What would be the best way to load 8 images into a program and have them move across the screen?
- Next by Date: InitInstance and exiting application
- Previous by thread: Re: Template classes as arguments in function pointers
- Next by thread: Re: Question about design?
- Index(es):
Relevant Pages
|