I have a class hierarchy where I know that a given class (B) will always be derived into a second one (D). In B's constructor, is it safe to statically cast the this
pointer into a D* if I'm sure that nobody will ever try to use it before the entire construction is finished? In my case, I want to pass a reference to the object to yet another class (A).
struct A
{
D & d_;
A(D & d) : d_(d) {}
};
struct D; //forward declaration
struct B
{
A a;
B() : a(std::static_cast<D&>(*this)) {}
};
struct D : public B
{};
Is this code safe?