Re: Question about design?



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


.



Relevant Pages

  • Re: Which compiler for a learner of C?
    ... Actually, while it looks nice enough, it doesn't compile my existing C ... Conversion from 'void*' to pointer to non-'void' requires an ...
    (comp.lang.c)
  • Re: Null pointers
    ... > I'm going to use the notation $12345678$ to refer to a pointer whose ... Remember that a null pointer constant is a source ... Conversion is done according to exactly the same rules as all other ... It is conceptually not a compile time construct. ...
    (comp.lang.c)
  • Re: Which compiler for a learner of C?
    ... Actually, while it looks nice enough, it doesn't compile my existing C ... Conversion from 'void*' to pointer to non-'void' requires an ...
    (comp.lang.c)
  • Re: Which compiler for a learner of C?
    ... Actually, while it looks nice enough, it doesn't compile my ... Conversion from 'void*' to pointer to non-'void' ...
    (comp.lang.c)
  • Re: ptr conversions and values
    ... >> each pointer pointed to a byte, ... Without that conversion, a pointer that points to an N-byte ... pointers to incomplete array types must indicate the start of the array ... >> It's all right, mostly equivalent with the previous one, representation ...
    (comp.std.c)