struct B {}; // B contains data members
struct D : B {}; // D doesn't contain ANY data member
B g_b; // global object
D& fun () // want to return by reference ONLY
{
return <???>(g_b); // how ???
}
[Note: I want to avoid overloading constructor (or assignment) such as D(const B&).]
That is undefined behavior.
You can use
dynamic_castfor performing safe down casting of Base class pointer/reference to derived class pointer/reference. It returns a null in case of pointers or throws an exception in case of references.