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

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

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


当前回答

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

其他的回答说:

公共-所有人都可以访问。 Protected -派生类(和友类)可以访问。 私人限制。

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

c++的核心准则给出了数据应该始终是私有的建议。我认为这是一个很好的建议,因为当你的派生类可以访问受保护的数据时,它会导致“数据面条”。保护函数更有意义,但这取决于用例。

对于函数,你有一个选择。对于数据,应该将其设置为私有,并在需要时提供受保护的访问函数。这样可以更好地控制类数据。

其他回答

这完全取决于您想要做什么,以及您希望派生类能够看到什么。

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

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: 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.

Private:可由类成员函数和好友函数或好友类访问。 对于c++类,这是默认的访问说明符。

Protected:可由类成员函数、友元函数或友元类及派生类访问。

您可以根据需要将类成员变量或函数(甚至是typedefs或内部类)保留为私有或受保护。 大多数情况下,保持类成员为私有,并添加get/set函数进行封装。这有助于代码的维护。 一般来说,当你想要保持公共函数模块化或消除重复代码而不是将整个代码写入单个函数时,就会使用私有函数。这有助于代码的维护。

更多细节请参考这个链接。

由于不需要公共成员函数来获取和更新派生类中的受保护成员,这提高了代码的效率,并减少了需要编写的代码量。然而,派生类的程序员应该知道他在做什么。