V
verec
I just do not understand this error.
Am I misusing dynamic_cast ?
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:bject*' (source is not a pointer to class)
file envelope.hpp
namespace mom {
// ...
void construct(void * t) ; // <-- decl
template <typename T> struct envelope {
// ...
static void * cast(T * t) {
return dynamic_cast<void *>(t) ;
}
// ...
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?
Many thanks
Am I misusing dynamic_cast ?
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:bject*' (source is not a pointer to class)
file envelope.hpp
namespace mom {
// ...
void construct(void * t) ; // <-- decl
template <typename T> struct envelope {
// ...
static void * cast(T * t) {
return dynamic_cast<void *>(t) ;
}
// ...
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?
Many thanks