Safe to cast pointer to a forward-declared class to its true base class in C++?
In one header file I have:
#include "BaseClass.h"
// a forward declaration of DerivedClass, which extends class BaseClass.
class DerivedClass ;
class Foo {
DerivedClass *derived ;
void someMethod() {
// this is the cast I'm worried about.
((BaseClass*)derived)->baseClassMethod() ;
}
};
Now, DerivedClass is (in its own header file) derived from BaseClass, but the compiler doesn't know that at the time it's reading the definition above for class Foo. However, Foo refers to DerivedClass pointers and DerivedClass refers to Foo pointers, so they can't both know each other's declaration.
First question is whether it's safe (according to C++ spec, not in any given compiler) to cast a derived class pointer to its base class pointer type in the absence of a full definition of 开发者_高级运维the derived class.
Second question is whether there's a better approach. I'm aware I could move someMethod()'s body out of the class definition, but in this case it's important that it be inlined (part of an actual, measured hotspot - I'm not guessing).
It may work, but the risk is huge.
The problem is that most of the times Derived*
and Base*
will indeed have the same value under the hood (which you could print). However this is not true as soon as you have virtual inheritance and multi-inheritance and is certainly not guaranteed by the standard.
When using static_cast
the compiler performs the necessary arithmetic (since it knows the layout of the class) to adjust the pointer value. But this adjustment is not performed by reinterpret_cast
or the C-style cast.
Now, you could perfectly rework your code like so:
// foo.h
class Derived;
class Foo
{
public:
void someMethod();
private:
Derived* mDerived;
};
// foo.cpp
#include "myProject/foo.h"
#include "myProject/foo.cpp"
void Foo::someMethod() { mDerived->baseClassMethod(); }
Anyway: you are using a pointer to Derived
, while it's okay there is a number of gotchas you should be aware of: notably if you intend to new
an instance of the class it will be necessary for you to redefine the Copy Constructor
, Assignment Operator
and Destructor
of the class to properly handle the pointer. Also make sure to initialize the pointer value in every Constructor
(whether to NULL or to an instance of Derived
).
It's not impossible but document yourself.
You should be using c++ style casts, not c-style. The reason being that what you are doing is actually a reinterpret_cast, which is, short of some few rare uses, almost universally unsafe.
The cast you are performing is not being done correctly. You might be able to cast between the two safely, this is implementation defined (only void* is guaranteed by the standard to be able to hold pointers to any type), but if you actually use the new pointer afterward you will summon nasal demons.
Most of the time these nasal demons are benign and you won't notice them (on a fair amount of implementations that is). If you use multiple inheritance, however, these same nasal demons will become quite devious. Even so, you should never accept undefined behavior as "safe" or anything other than inherently bad unless there's simply no other way to get what you need (which as a professional I've never run into).
Which brings up the whole reason NOT to use c-style casts. There are some very, very few and very rare conditions when you have to but as a professional I've never run into one. The problem with c-style casts is that they will silently change to a different type of cast depending on the semantics of the situation. Minor code changes, in separate areas of code, can severely alter the cast operation without your knowledge. A c++ style cast will inform you when the semantics have changed to require a different type of cast, a c-style cast will simply silently change; when using c-style casts you can go from defined to undefined behavior without any kind of clue that it's happened.
What you have here is a reinterpret_cast
in C++'s terms (because a "known to be safe" static_cast
would result in a compiler error), which I would be very suspicious of.
Wouldn't it be possible to forward declare Foo for Derived instead?
精彩评论