提问者:小点点

C++派生类正在访问基类的私有成员,而不是它自己的私有成员


很抱歉,如果这是一个明显的问题,但我已经找遍了,我仍然不清楚如何解决这个问题。下面是我的代码:

#include <iostream>
#include <string>
using namespace std;

class PermMagnet {
public:
    string name;
    int ac_rating;
    int dc_rating;
    int mass_kg;
    int age;
    PermMagnet(){
        // default constructor
        name = "";
        ac_rating = 0; dc_rating = 0;
        mass_kg = 0; age = 0;
    }
    PermMagnet(string c_name, int c_ac_rating, int c_dc_rating, int c_mass_kg, int c_age){
        // parameterised constructor
        name = c_name;
        ac_rating = c_ac_rating;
        dc_rating = c_dc_rating;
        mass_kg = c_mass_kg;
        age = c_age;
    }
    string get_owner(){
        return owner;
    }
    string get_classifier(){
        return classifier;
    }
    int get_coil_count(){
        return coil_num;
    }
protected:
    string location = "facility hall";
private:
    string owner = "Unspecified Staff";
    string classifier = "MAG-DP-";
    const int coil_num = 2;
};

class ElecMagnet : public PermMagnet {
public:
    // inherit base class constructors
    using PermMagnet::PermMagnet;

    string get_location(){
        return location;
    }

private:
    string owner = "Specified Staff";
    string classifier = "MAG-QD-";
    const int coil_num = 4;
};

int main() {

    // Create object using default constructor
    PermMagnet perm1;
    cout << "'perm1' age: " << perm1.age << endl;

    // Create object using parameterised constructor
    PermMagnet perm2("PermMagnet 2", 380, 400, 1500, 35);
    cout << "'perm2' age: " << perm2.age << " | 'perm2' name: " << perm2.name << endl;
    cout << "Owner of 'perm2': " << perm2.get_owner() << endl;
    cout << "Upper current bound of 'perm2': " << perm2.get_current_limit("upper") << "A" << endl;
    cout << "Number of coils in 'perm2': " << perm2.get_coil_count() << endl << endl;

    // Create a ElecMagnet (derived class) object
    ElecMagnet elec1("ElecMagnet 1", 170, 200, 850, 27);
    cout << elec1.get_classifier() << endl;
    cout << elec1.get_coil_count() << endl;

    return 0;
}

此代码的输出为:

'perm1' age: 0
'perm2' age: 35 | 'perm2' name: PermMaget 2
Owner of 'perm2': Unspecified Staff
Upper current bound of 'perm2': 780A
Number of coils in 'perm2': 2

MAG-DP-
2

Process finished with exit code 0

如您所见,我希望“owner”,“classifier”和“coil_num”是用户不能更改的私有成员。这些也因所讨论的类别而异。

问题是:

问题出在输出的最后两行。当派生类(ElectMagnet)继承返回这些成员的公共函数时,它返回基类的成员;不是自己的。您可以看到这一点,因为它返回PermMagnet类的“classifier”和“coil_num”。

有人知道派生类为什么会这样做吗?难道它不应该访问它自己的私有成员而不是基地吗?


共2个答案

匿名用户

私有成员只能由同一类或朋友的成员函数访问。C++的行为不像Python,在Python中,根据当前指针this(self)动态执行变量搜索。

匿名用户

发生这种情况是因为您仍在调用基类的方法,而该方法无法了解派生类的成员变量,因此不会使用它们。要存档所需内容,需要重写派生类中的get_classifier(),以便通过elecmagnet-reference调用它将调用派生方法。当通过PermMagnet-reference在elecMagnet-value上调用该方法时,您可以选择使该方法成为virtual,甚至获得调用的派生版本。(通常建议将设计用于继承的类创建为虚拟。)

或者,根据您的用例,不使用使用基构造函数,您可以编写显式构造函数,用分类器的适当值调用(可能是受保护的)基构造函数。这将完全节省派生类中的其他成员变量。

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(c++|派生类|访问|基|类|私有|成员|它自己|私有|成员)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?