c++类中的私有成员和受保护成员有什么区别?
我从最佳实践惯例中了解到,没有在类外部调用的变量和函数应该是私有的,但看看我的MFC项目,MFC似乎更倾向于受保护。
有什么区别,我应该用哪个?
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 A
{
private:
int _privInt = 0;
int privFunc(){return 0;}
virtual int privVirtFunc(){return 0;}
protected:
int _protInt = 0;
int protFunc(){return 0;}
public:
int _publInt = 0;
int publFunc()
{
return privVirtFunc();
}
};
class B : public A
{
private:
virtual int privVirtFunc(){return 1;}
public:
void func()
{
_privInt = 1; // wont work
_protInt = 1; // will work
_publInt = 1; // will work
privFunc(); // wont work
privVirtFunc(); // will work, simply calls the derived version.
protFunc(); // will work
publFunc(); // will return 1 since it's overridden in this class
}
}
当然,看看受保护的成员变量问题。建议使用private作为默认值(就像c++类一样)来减少耦合。受保护的成员变量总是一个坏主意,受保护的成员函数可以用于例如模板方法模式。
私有成员只能在定义它们的类中访问。
可在定义受保护成员的类中以及从该类继承的类中访问受保护成员。
编辑:两者都可以被其类的友元访问,如果是受保护成员,则可以被其派生类的友元访问。
编辑2:使用任何在你的问题上下文中有意义的东西。您应该尽可能地将成员设为private,以减少耦合并保护基类的实现,但如果不可能,则使用受保护的成员。查看c++常见问题以更好地理解这个问题。这个关于受保护变量的问题可能也有帮助。
与私有属性和方法不同,标记为受保护的属性和方法在子类中仍然可见。
除非您不想在可能的子类中使用或提供重写该方法的可能性,否则我会将它们设为private。
成员数据首选Private。c++类中的成员默认是私有的。
Public是成员函数的首选,尽管这是一个意见问题。至少有一些方法必须是可访问的。公共是对所有人开放的。这是最灵活的选择,也是最不安全的选择。任何人都可以使用它们,任何人都可以滥用它们。
Private根本无法访问。没有人可以在课堂之外使用它们,也没有人可以滥用它们。甚至在派生类中也没有。
Protected是一种折衷,因为它可以在派生类中使用。当您从类派生时,您对基类有很好的理解,并且注意不要误用这些成员。
MFC是Windows API的c++包装器,它更倾向于public和protected。由Visual Studio向导生成的类包含受保护的、公共的和私有的成员。但是MFC类本身有一些逻辑。
像SetWindowText这样的成员是公共的,因为您经常需要访问这些成员。
成员如OnLButtonDown,处理窗口接收到的通知。它们不应该被访问,因此受到保护。您仍然可以在派生类中访问它们以重写这些函数。
一些成员必须执行线程和消息循环,它们不应该被访问或覆盖,因此它们被声明为私有
在c++结构中,成员默认是公共的。结构通常只用于数据,而不是方法,因此公共声明被认为是安全的。