简体   繁体   English

C ++访问说明符的理解

[英]c++ access-specifier understanding

I have encountered the following response in a thread : 我在线程中遇到以下响应:

Protected members can be accessed from derived classes. 可以从派生类访问受保护的成员。 Private ones can't. 私人的不能。

class Base {

private: 
  int MyPrivateInt;
protected: 
  int MyProtectedInt;
public:
  int MyPublicInt;
};

class Derived : Base
{
public:
  int foo1()  { return MyPrivateInt;} // Won't compile!
  int foo2()  { return MyProtectedInt;} // OK  
  int foo3()  { return MyPublicInt;} // OK
};

class Unrelated 
{
private:
  Base B;
public:
  int foo1()  { return B.MyPrivateInt;} // Won't compile!
  int foo2()  { return B.MyProtectedInt;} // Won't compile
  int foo3()  { return B.MyPublicInt;} // OK
};

... ...

1) my question is : I have read : "A class derivation list names one or more base classes and has the form: 1) 我的问题是 :我已阅读:“类派生列表命名一个或多个基类,其形式为:

class derived-class: access-specifier base-class 类派生类:访问说明符基类

Where access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. 其中access-specifier是public,protected或private之一,而base-class是先前定义的类的名称。 If the access-specifier is not used, then it is private by default. 如果未使用访问说明符,则默认情况下为私有。 " and "Private Inheritance: When deriving from a private base class, public and protected members of the base class become private members of the derived class. 和“私有继承:从私有基类派生时,基类的公共成员和受保护成员将成为派生类的私有成员。

"

SO...in our example class Derived : Base is equivalent to class Derived : private Base because no access-specifier has been defined, yet the code works as the writer said, so what am i missing?- i thought that Base class for the class Derived access-specifier is private therefore public and protected members of Base should be private for class Derived and can't be accessed... thanks! 所以...在我们的示例中,类Derived:Base与类Derived:private Base等效,因为尚未定义访问说明符,但代码按作者所说的工作,所以我错过了什么?-我认为该Base类适用于class Derived访问说明符是私有的,因此Base的public和受保护的Base成员对于Derived类应该是私有的,因此无法访问...谢谢!

Its a similar kind of idea. 这是类似的想法。 Rather than applying to which members of the class you can access, it applies to which base classes you can access. 与其适用于您可以访问该类的哪些成员,不如适用于您可以访问哪些基类。

class A
{
public:
    void f();
};



class B : public A
{
public:
    void g()
    {
        f(); // OK
        A *a = this; // OK
    }
};
class B2 : public B
{
public:
    void h()
    {
        f(); //OK
        A *a = this; // OK
    };
};
B b;
A& ba = b;


class C : protected A
{
public:
    void g()
    {
        f(); // OK, can access protected base
        A *a = this; // OK, can access protected base
    }
};
class C2 : public C
{
public:
    void h()
    {
        f(); // OK, can access protected base
        A *a = this; // OK, can access protected base
    };
};
C c;
c.f(); // Not OK, allthough A::f() is public, the inheritance is protected.
A& ca = c; // Not OK, inheritence is protected.




class D : private A
{
public:
    void g()
    {
        f(); // OK because A is a base of D
        A *a = this;
    }
};
class D2 : public D
{
public:
    void h()
    {
        f(); //Not OK, A is inherited with private in D
        A *a = this; //Not OK
    };
};
D d;
d.f(); // Not OK, allthough A::f() is public, the inheritance is private.
D& da = d; // Not OK, inheritence is private.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM