Should I pointer-cast from a private derived class to its base class?

Posted by skydoor on Stack Overflow See other posts from Stack Overflow or by skydoor
Published on 2010-03-21T19:31:58Z Indexed on 2010/03/21 19:41 UTC
Read the original article Hit count: 103

Filed under:

I found this from C++FAQ

Generally, No.

From a member function or friend of a privately derived class, the relationship to the base class is known, and the upward conversion from PrivatelyDer* to Base* (or PrivatelyDer& to Base&) is safe; no cast is needed or recommended.

However users of PrivatelyDer should avoid this unsafe conversion, since it is based on a private decision of PrivatelyDer, and is subject to change without notice.

How to understand the above words? I don't think the explanation is correct or accurate.

I have a code like this

class A{
};

class B: private A{
};

int main(){

    B *b = new B();
    A *a = new A();

    a = b;                    //wrong
    a = (A*)b;            //right

}

© Stack Overflow or respective owner

Related posts about c++