Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 继承子类时的默认保护级别是什么?_C++_Inheritance - Fatal编程技术网

C++ 继承子类时的默认保护级别是什么?

C++ 继承子类时的默认保护级别是什么?,c++,inheritance,C++,Inheritance,可能重复: 我知道,当我从超类声明子类时,我可以设置保护级别,如下所示: class Dog : public Pet { *blah, blah, blah* } 但在这种情况下,保护级别默认为什么 Class Dog: Pet { *blah, blah, blah* } 对于一个班级来说,这是私人的 class Dog: Pet // Pet is inherited privately. {} 对于结构,它是公共的 struct Dog: Pet // Pet i

可能重复:

我知道,当我从超类声明子类时,我可以设置保护级别,如下所示:

class Dog : public Pet {
   *blah, blah, blah*
}
但在这种情况下,保护级别默认为什么

Class Dog: Pet {
   *blah, blah, blah*
}

对于一个班级来说,这是私人的

class Dog: Pet  // Pet is inherited privately.
{}
对于结构,它是公共的

struct Dog: Pet  // Pet is inherited publicly.
{}
简单测试:

class Pet {};
class  DogClass:  Pet {};
struct DogStruct: Pet {};
int main()
{
    DogClass   dogClass;
    // Pet&       pet1 = dogClass;  This line will not compile.

    DogStruct  dogStruct;
    Pet&       pet2 = dogStruct;
}

对于一个班级来说,这是私人的

class Dog: Pet  // Pet is inherited privately.
{}
对于结构,它是公共的

struct Dog: Pet  // Pet is inherited publicly.
{}
简单测试:

class Pet {};
class  DogClass:  Pet {};
struct DogStruct: Pet {};
int main()
{
    DogClass   dogClass;
    // Pet&       pet1 = dogClass;  This line will not compile.

    DogStruct  dogStruct;
    Pet&       pet2 = dogStruct;
}

你有编译器吗?你就不能测试一下这些东西吗?@Andrei:当你知道怎么做的时候,测试就很容易了。但是看起来OP知道怎么做测试吗?从问题本身来看,这似乎不太可能。现在,解释一个简单的测试(比如绑定一个引用并看到编译失败)可能对你很有用。@Martin:你可以在回答中添加一个测试来证明这一点(包括必要的
//不会编译
注释等)。你有编译器吗?你就不能测试一下这些东西吗?@Andrei:当你知道怎么做的时候,测试就很容易了。但是看起来OP知道怎么做测试吗?从问题本身来看,这似乎不太可能。现在,解释一个简单的测试(比如绑定一个引用并看到编译失败)可能对你很有用。@Martin:你可以在答案中添加一个测试来证明这一点(包括必要的
//不会编译
注释等)。