L*******e 发帖数: 114 | 1 Here is the code snippet from C++ programing language book. What confused me
is the statement "px = py2", the book says an error "we do not know that
py2 is a Z2 or how Y2::x is used in an non-Z2 object", but g++ did not
complain.
Did I misread something?
class X{
public:
int a;
};
class Y1 : public X {};
class Y2 : protected X {};
class Y3 : private X {};
class Z2 : public Y2 {
void f(Y1* py1, Y2* py2, Y3* py3)
{
X *px = py1; // X is a public base of Y1
py1->a = 7 | j***i 发帖数: 1278 | 2 I think px=py2 should be fine, py2 is the argument Y2 obj's ptr
do not related to Z2 , and px->a=7 is also fine, since the access check is
static, px is X* so a is accessible
me
【在 L*******e 的大作中提到】![](/moin_static193/solenoid/img/up.png) : Here is the code snippet from C++ programing language book. What confused me : is the statement "px = py2", the book says an error "we do not know that : py2 is a Z2 or how Y2::x is used in an non-Z2 object", but g++ did not : complain. : Did I misread something? : class X{ : public: : int a; : }; : class Y1 : public X {};
| x***y 发帖数: 633 | 3 I think this is an interesting problem for the conversion between
inheritence hiearachy.
class Base{};
class publicDerived: public Base{};
class protectedDerived: protected Base{};
class privateDerived: private Base{};
Base * pBase;
publicDerived * pPublic;
protectedDerived * pProtected;
privateDerived * pPrivate;
pBase = pPublic; // fine
pBase = pProtected; //error C2243: 'type cast' : conversion exists, but is
inaccessible
pBase = pPrivate; // error C2243: 'type cast' : conversion exists, but
【在 L*******e 的大作中提到】![](/moin_static193/solenoid/img/up.png) : Here is the code snippet from C++ programing language book. What confused me : is the statement "px = py2", the book says an error "we do not know that : py2 is a Z2 or how Y2::x is used in an non-Z2 object", but g++ did not : complain. : Did I misread something? : class X{ : public: : int a; : }; : class Y1 : public X {};
|
|