简体   繁体   English

struct和class and inheritance(c ++)

[英]struct and class and inheritance (c++)

Could you ensure me, if all access specifiers (including inheritance) in struct are public ? 如果struct中的所有访问说明符(包括继承)都是public ,你能确定我吗?

In other words: are those equal? 换句话说:那些是平等的吗?

class C: public B, public A { public:
    C():A(1),B(2){}
    //...
};

and

struct C: B, A {
    C():A(1),B(2){}
    //...
};

Yes, they all are public. 是的,他们都是公开的。

struct A : B {
  C c;
  void foo() const {}
}

is equivalent to 相当于

struct A : public B {
 public:
  C c;
  void foo() const {}
}

For members, it is specified in §11: 对于成员,它在§11中指定:

Members of a class defined with the keyword class are private by default. 默认情况下,使用关键字class定义的类的成员是私有的。 Members of a class defined with the keywords struct or union are public by default. 默认情况下,使用关键字struct或union定义的类的成员是公共的。

and for for base classes in §11.2: 对于§11.2中的基类:

In the absence of an access-specifier for a base class, public is assumed when the derived class is defined with the class-key struct and private is assumed when the class is defined with the class-key class. 在缺少基类的访问说明符时,假定使用类 - 键结构定义派生类时使用public,并且在使用类 - 键类定义类时假定使用private。

where the references are to the C++11 standard. 其中引用的是C ++ 11标准。

From C++ standard , 11.2.2, page 208: C ++标准 ,11.2.2,第208页:

In the absence of an access-specifier for a base class, public is assumed when the derived class is declared struct and private is assumed when the class is declared class. 在没有基类的访问规范的情况下,当派生类被声明为struct时假定为public,并且当类被声明为class时假定为private。

So yes, you are correct: when the derived class is a struct , it inherits other classes as public unless you specify otherwise. 所以是的,你是对的:当派生类是一个struct ,它继承其他类作为public除非你另外指定。

From the C++11 Standard ( N3242 draft ) 来自C ++ 11标准( N3242草案

11.2 Accessibility of base classes and base class members 11.2基类和基类成员的可访问性

2 In the absence of an access-specifier for a base class, public is assumed when the derived class is defined with the class-key struct and private is assumed when the class is defined with the class-key class. 2如果没有基类的访问说明符,则在使用类 - 键结构定义派生类时假定为public,并且在使用类 - 键类定义类时假定为private。

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

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