verec wrote:[color=blue]
> I just do not understand this error.
> Am I misusing dynamic_cast ?[/color]
yes. you can't cast from void* because the compiler can't know what
type the object really is. you can only cast from polymorphic types (in
which case, the compiler typically looks at the v-table pointer to
determine what type the object really is).
[color=blue]
> What I want to do is to have a single template construct
> (with no optional argument) so that it works for whatever
> T I want to use it with. Now, if that T happens to be some
> subclass of a known base class (object, in this case), I
> want to perform some extra stuff ...
>
> I've read Faq#35, and the most natural solution would have
> been to write the function as a non static envelope member
> parameterized on T, but the reason the code is "out-line" is
> because of a circular dependency that would be introduced
> between object & envelope if I was to let envelope.hpp know
> about the innards of object.hpp.
>
> I solved this by using a "free standing", non templatized regular
> function, whose definition, inside envelope.cpp is now allowed
> to include object.hpp ...
>
> /Users/verec/Tools/trunk/Style/tests/build/mom/../../../src/common/mom/envelope.cpp:68:
> error:
>
> cannot dynamic_cast 't' (of type 'void*') to type
> 'struct mom::object*' (source is not a pointer to class)
>
> file envelope.hpp
>
> namespace mom {
> // ...
> void construct(void * t) ; // <-- decl[/color]
void construct(void * t);
void construct(object * t);
problem solved?
[color=blue]
>
> template <typename T> struct envelope {
>
>
> // ...
>
> static void * cast(T * t) {
>
> return dynamic_cast<void *>(t) ;[/color]
the cast is completely unnecessary, T* converts to void* implicitly,
where T is not a function or member function type. of course, if you
convert to void*, you lose all type info, which probably isn't a good
thing.
you might want to consider boost::any, however that probably won't help
with your overall design.
www.boost.org
[color=blue]
> }
>
> // ...
>
> envelope(T * q = 0) : body(q) {
> // ...
> construct(cast(body)) ;
>
> }
> } ;
> }
>
> file envelope.cpp
>
> #include "mom/envelope.hpp"
> #include "mom/object.hpp"
>
> namespace mom {
>
> // ...
> void
> construct(void * t) {
> object * o = dynamic_cast<object *>(t) ; // <-- defn: where
> GCC 4.0 chokes.
> if (o) {
> o->construct() ;
> }
> }
> }
>
> for completeness ...
> file opbject.hpp
>
> namespace mom {
> struct object {
> object() {}
>
> virtual
> ~object() {}
>
> virtual void
> construct() {}
> // ...
> } ;
> }
>
> Any idea what I'm doing wrong?[/color]
quite frankly, any program design which includes such an incredibly
generic object base class usually smells of a bad design. you can't do
anything with this object other than construct or destroy it. you'd
have to cast to a more specific derived class type to do anything
useful with it, which trying to figure out what derived class to cast
to is generally a "fun" exercise. C++ doesn't have an object base class
for a reason.[color=blue]
>
> Many thanks
> --
> JFB[/color]