C++ 默认继承访问说明符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4796789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:32:08  来源:igfitidea点击:

Default inheritance access specifier

c++inheritance

提问by Simplicity

If I have for example two classes Aand B, such that class Binherits Aas follows:

例如,如果我有两个类Aand B,则该类B继承A如下:

class B: public A

class B: public A

In this case, I'm doing publicinheritance.

在这种情况下,我正在做public继承。

If I write the previous code as follows:

如果我把前面的代码写成如下:

class B: A

class B: A

What type of inheritance will I be doing here (i.e; public)? In other words, what is the default access specifier?

我将在这里进行什么类型的继承(即公共)?换句话说,默认访问说明符是什么?

Just a side question here. Do I call the previous line of codes statements? Especially that I remember I read in the C++ Without Fear: A Beginner's Guide That Makes You Feel Smartbook that statementsare that that end with ;. What do you think about that?

这里只是一个附带问题。我调用前一行代码statements吗?特别是我记得我读过C++ 无所畏惧:让你感觉聪明的初学者指南这本书statements;. 你怎么看?

Thanks.

谢谢。

回答by anatolyg

Just a small addition to all the existing answers: the default type of the inheritance depends on the inheriting (derived) type (Bin the example), not on the one that is being inherited (base) (Ain the example).

只是对所有现有答案的一小部分补充:继承的默认类型取决于继承(派生)类型(B在示例中),而不取决于正在继承的类型(基础)(A在示例中)。

For example:

例如:

class A {};
struct B: /* public */ A {};
struct A {};
class B: /* private */ A {};

回答by ybungalobill

It's private for class and public for struct.

它对于类是私有的,对于结构是公共的。

Side answer: No, these are definitionsof the class according to the standard. Class definition end with a semicolon. On the other hand not all statements end with a semicolon (e.g. an ifstatement does not).

旁答:不,这些是根据标准对类的定义。类定义以分号结尾。另一方面,并​​非所有语句都以分号结尾(例如,if语句不以分号结尾)。

回答by sbi

If you use classto define your class, the default access specifier will be private. (I think it's wrong, too.) If you use struct, however, it will be public.

如果class用于定义类,则默认访问说明符将为private. (我也认为这是错误的。)struct但是,如果您使用,它将是public.

And class definitions are declarations, I think. A statement is what translates into actual code (unless optimized away, anyway).
However, a mildly exotic feature of C and C++ is that expressions are statements. That's why 3+4;is a syntactically legal statement in C++ (although many compilers will warn about it having no effect). While it is obviously nonsense in this case, in general expressions are evaluated for their side effects. (An obvious example is discarding a function's return value. You call the function not to obtain a result, but for its side effects.)

我认为,类定义是声明。语句是转化为实际代码的内容(除非优化掉,无论如何)。
然而,C 和 C++ 的一个有点奇怪的特性是表达式是语句。这就是为什么3+4;在 C++ 中是一个语法上合法的语句(尽管许多编译器会警告它没有效果)。虽然在这种情况下这显然是无稽之谈,但通常表达式会评估它们的副作用。(一个明显的例子是丢弃函数的返回值。调用函数不是为了获得结果,而是为了它的副作用。)

回答by Pradip Khomane

When you inherit a class from another class, then default access specifier is private.

当您从另一个类继承一个类时,默认访问说明符是私有的。

#include <stdio.h>

class Base {
public:
int x;
};

class Derived : Base { }; // is equilalent to class Derived : private Base       {}

int main()
{
 Derived d;
 d.x = 20; // compiler error becuase inheritance is private
 getchar();
 return 0;
}

When you inherit a structure from another class, then default access specifier is public.

当您从另一个类继承结构时,默认访问说明符是 public。

#include < stdio.h >
  class Base {
    public:
      int x;
  };

struct Derived: Base {}; // is equilalent to struct Derived : public Base {}

int main() {
  Derived d;
  d.x = 20; // works fine becuase inheritance is public
  getchar();
  return 0;
}

回答by StoryTeller - Unslander Monica

The "type" of inheritance depends on how the class is defined. There are default access specifiers applied to inheritance. From the C++ standard:

继承的“类型”取决于类的定义方式。有应用于继承的默认访问说明符。从 C++ 标准:

[class.access.base]/2

[class.access.base]/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 structand private is assumed when the class is defined with the class-key class. [?Example:

class B { /* ... */ };
class D1 : private B { /* ... */ };
class D2 : public B { /* ... */ };
class D3 : B { /* ... */ };             // B private by default
struct D4 : public B { /* ... */ };
struct D5 : private B { /* ... */ };
struct D6 : B { /* ... */ };            // B public by default
class D7 : protected B { /* ... */ };
struct D8 : protected B { /* ... */ };

Here B is a public base of D2, D4, and D6, a private base of D1, D3, and D5, and a protected base of D7 and D8. ?—?end example?]

在没有基类的访问说明符的情况下,当派生类使用 class-key 定义时假定为 public,而当使用 class-key 定义类struct时假定为 private class。[?例子:

class B { /* ... */ };
class D1 : private B { /* ... */ };
class D2 : public B { /* ... */ };
class D3 : B { /* ... */ };             // B private by default
struct D4 : public B { /* ... */ };
struct D5 : private B { /* ... */ };
struct D6 : B { /* ... */ };            // B public by default
class D7 : protected B { /* ... */ };
struct D8 : protected B { /* ... */ };

这里B是D2、D4和D6的公共基地,D1、D3和D5的私人基地,以及D7和D8的保护基地。?—?结束示例?]

回答by Dev

The default type of the inheritance is privatein C++.

继承的默认类型在 C++ 中是私有的

class B:A
{};

is equivalent to

相当于

class B: private A
{};

回答by James

If you do not choose an inheritance, C++ defaults to privateinheritance in the same way class members default to privateaccess for classes.

如果您不选择继承,C++ 默认private继承的方式与类成员默认private访问类的方式相同。

回答by Sulla

the default access specifier is an important differentiator between classes and structs. It is public by default for structs and private by default for classes.

默认访问说明符是类和结构之间的重要区别。默认情况下,它对于结构是公共的,对于类默认是私有的。

回答by Sulla

AS other casting problem you have

作为您遇到的其他铸造问题

class A { virtual void test() = 0; };
class B : virtual public A { virtual void testb() {} };
class C : virtual public A { virtual void testc() {} };
class D : public B, public C {
 virtual void test() override {}
}

void main() {
   D d;
   void* v = &d;
   A* a = &d;

   ((D*)A)->test(); //OK
   ((D*)v)->test(); //undefined behavior (that call testb() in vtable logic at 1st inheritance position)
   dynamic_cast<D*>(v)->test(); //compile error cast from void* not permitted

   //resolution
   void* x = a;
   ((D*)x)->test(); //OK but as you can see, you must to store a* in x*

}