gpt4 book ai didi

c++ - 结构和类以及继承 (c++)

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:05:19 27 4
gpt4 key购买 nike

如果 struct 中的所有访问说明符(包括继承)都是 public,你能向我保证吗?

换句话说:它们相等吗?

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

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

最佳答案

是的,它们都是公开的。

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

相当于

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

对于成员,在 §11 中指定:

Members of a class defined with the keyword class are private by default. Members of a class defined with the keywords struct or union are public by default.

对于 §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.

其中引用了 C++11 标准。

关于c++ - 结构和类以及继承 (c++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10930091/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com