Given a base/derived class polymorphic relationship (established below via the virtual destructor), if I explicitly (C-style) cast a pointer-to-a-base object to a pointer-to-a-derived object, is this ill-formed/undefined? I know this is not good practice.
In this specific example (the one I am primarily concerned about), the pointed-to object is actually a Base
, and the Derived
class has no data, only newly defined functions. Does this make a difference?
class Base
{
public:
Base()
: data(1)
{};
virtual ~Base(){};
protected:
int data;
};
class Derived : public Base
{
public:
Derived(){};
virtual ~Derived(){};
int new_function(){return data;};
};
int main()
{
Base b;
return ((Derived *)&b)->new_function(); //undefined?
}
From my understanding of explicit casting, this would result in an static_cast
. The static_cast
explanation states:
if expression is actually not a base class subobject of an object of type Derived, the behavior is undefined.
My guess here is that the Base class is not a subobject of the Derived in my example, and so the result is undefined, but I'm not sure. I know a dynamic_cast
will fail (nullptr/exception), which makes sense because the dynamic type here is a Base
.
If there is specific information on the VS2010 VC++ compiler-specific behavior, that's a bonus.
I tried running this on https://www.onlinegdb.com/online_c++_compiler and it functions, but I know this doesn't mean it's ill-formed and could be compiler-specific.
Your code has undefined behavior:
See C++ standard (e.g., §10.3.7 in C++17), accessing a derived class member function through a base class object that was not constructed as the derived type results in undefined behavior.