c++类中的私有成员和受保护成员有什么区别?

我从最佳实践惯例中了解到,没有在类外部调用的变量和函数应该是私有的,但看看我的MFC项目,MFC似乎更倾向于受保护。

有什么区别,我应该用哪个?


当前回答

受保护的非静态基类成员可以被派生自该基类的任何类的成员和友元访问,方法是使用以下方法之一:

指向直接或间接派生类的指针 对直接或间接派生类的引用 直接或间接派生类的对象

其他回答

Private: It is an access specifier. By default the instance (member) variables or the methods of a class in c++/java are private. During inheritance, the code and the data are always inherited but is not accessible outside the class. We can declare our data members as private so that no one can make direct changes to our member variables and we can provide public getters and setters in order to change our private members. And this concept is always applied in the business rule. Protected: It is also an access specifier. In C++, the protected members are accessible within the class and to the inherited class but not outside the class. In Java, the protected members are accessible within the class, to the inherited class as well as to all the classes within the same package.

可以从派生类访问受保护的成员。私立学校则不行。

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
};

至于“最佳实践”,则视情况而定。如果有人想要从您现有的类中派生一个新类,并且需要访问内部成员,那么将它们设置为Protected,而不是Private。如果它们是私有的,那么您的类就很难轻易继承。

The protected keyword specifies access to class members in the member-list up to the next access specifier (public or private) or the end of the class definition. Class members declared as protected can be used only by the following: Member functions of the class that originally declared these members. Friends of the class that originally declared these members. Classes derived with public or protected access from the class that originally declared these members. Direct privately derived classes that also have private access to protected members. When preceding the name of a base class, the protected keyword specifies that the public and protected members of the base class are protected members of its derived classes. Protected members are not as private as private members, which are accessible only to members of the class in which they are declared, but they are not as public as public members, which are accessible in any function. Protected members that are also declared as static are accessible to any friend or member function of a derived class. Protected members that are not declared as static are accessible to friends and member functions in a derived class only through a pointer to, reference to, or object of the derived class.

保护(c++)

成员数据首选Private。c++类中的成员默认是私有的。

Public是成员函数的首选,尽管这是一个意见问题。至少有一些方法必须是可访问的。公共是对所有人开放的。这是最灵活的选择,也是最不安全的选择。任何人都可以使用它们,任何人都可以滥用它们。

Private根本无法访问。没有人可以在课堂之外使用它们,也没有人可以滥用它们。甚至在派生类中也没有。

Protected是一种折衷,因为它可以在派生类中使用。当您从类派生时,您对基类有很好的理解,并且注意不要误用这些成员。

MFC是Windows API的c++包装器,它更倾向于public和protected。由Visual Studio向导生成的类包含受保护的、公共的和私有的成员。但是MFC类本身有一些逻辑。

像SetWindowText这样的成员是公共的,因为您经常需要访问这些成员。

成员如OnLButtonDown,处理窗口接收到的通知。它们不应该被访问,因此受到保护。您仍然可以在派生类中访问它们以重写这些函数。

一些成员必须执行线程和消息循环,它们不应该被访问或覆盖,因此它们被声明为私有

在c++结构中,成员默认是公共的。结构通常只用于数据,而不是方法,因此公共声明被认为是安全的。

私有成员只能从类内部访问,受保护成员可以在类和派生类中访问。这是面向对象语言中继承的一个特性。

在c++中可以有私有、受保护和公共继承,这将决定哪些派生类可以在继承层次结构中访问。例如,c#只有公共继承。