在派生 class C++ 中访问受保护成员
Accessing protected members in derived class C++
void FemaleIn::enterPatientData()
{
cout << "enter name ";
cin >> this->name;
cout << "enter your age ";
cin >> this->age;
cout << "enter your diagnosis ";
cin >> this->diagnosis;
cout << "enter your insurance name ";
cin >> this->insuranceName;
cout << "enter your insurance number ";
cin >> this->insuranceNumber;
}
这是我的代码,这个函数在 FemaleIn class 中,它派生自 Female,但 Female 也派生自 patient。我想做的是我想在患者 class 中使用受保护的成员。没有错误,但是当我 运行 程序时,它被弄脏了。作为参考,我使用矢量来根据患者类型存储患者对象。像这样 std::vector 患者
class FemaleIn: virtual public Female, virtual public Inpatient
{
public:
FemaleIn();
void parse();
void toString();
void enterPatientData();
protected:
private:
};
class Female: virtual public Patient
{
public:
Female();
protected:
private:
};
class Patient
{
public:
Patient();
virtual void parse();
virtual void toString();
virtual void enterPatientData();
protected:
char* name;
char* SSN;
char* insuranceName;
char* insuranceNumber;
char* age;
char* spouseName;
char* diagnosis;
};
我的问题是如何将派生 class 中的每个值存储到基 class(patient) 中的成员变量?
仅根据您提供的代码,您似乎没有为 char *
成员变量分配任何内存来存储字符串。如果我的假设是正确的,那么您的程序将失败,因为它试图将字符串复制到未指向任何有效内存 space 的指针中,这会导致未定义的行为。我将为您提供可以解决问题的最少、最好、最安全的编辑。
class Patient
{
public:
Patient();
virtual void parse();
virtual void toString();
virtual void enterPatientData();
protected:
std::string name;
std::string SSN;
std::string insuranceName;
std::string insuranceNumber;
std::string age;
std::string spouseName;
std::string diagnosis;
};
将每个受保护成员变量的类型从 char *
更改为 std::string
现在将允许您从标准输入中读取字符串并将它们存储在每个成员变量中,并且 std::string
对象将根据需要处理所有必要的内存分配(以及在不再使用时清理它)。然后您应该能够按原样使用您的函数 FemaleIn::enterPatientData
因为语法是正确的。
除此之外,正如其他人所指出的,您可能需要重新考虑您的 class 层次结构设计,但这在这里应该不是问题。您可能还需要重新考虑如何存储某些类型的变量(例如,age
可能更好地存储为 int
)。
void FemaleIn::enterPatientData()
{
cout << "enter name ";
cin >> this->name;
cout << "enter your age ";
cin >> this->age;
cout << "enter your diagnosis ";
cin >> this->diagnosis;
cout << "enter your insurance name ";
cin >> this->insuranceName;
cout << "enter your insurance number ";
cin >> this->insuranceNumber;
}
这是我的代码,这个函数在 FemaleIn class 中,它派生自 Female,但 Female 也派生自 patient。我想做的是我想在患者 class 中使用受保护的成员。没有错误,但是当我 运行 程序时,它被弄脏了。作为参考,我使用矢量来根据患者类型存储患者对象。像这样 std::vector 患者
class FemaleIn: virtual public Female, virtual public Inpatient
{
public:
FemaleIn();
void parse();
void toString();
void enterPatientData();
protected:
private:
};
class Female: virtual public Patient
{
public:
Female();
protected:
private:
};
class Patient
{
public:
Patient();
virtual void parse();
virtual void toString();
virtual void enterPatientData();
protected:
char* name;
char* SSN;
char* insuranceName;
char* insuranceNumber;
char* age;
char* spouseName;
char* diagnosis;
};
我的问题是如何将派生 class 中的每个值存储到基 class(patient) 中的成员变量?
仅根据您提供的代码,您似乎没有为 char *
成员变量分配任何内存来存储字符串。如果我的假设是正确的,那么您的程序将失败,因为它试图将字符串复制到未指向任何有效内存 space 的指针中,这会导致未定义的行为。我将为您提供可以解决问题的最少、最好、最安全的编辑。
class Patient
{
public:
Patient();
virtual void parse();
virtual void toString();
virtual void enterPatientData();
protected:
std::string name;
std::string SSN;
std::string insuranceName;
std::string insuranceNumber;
std::string age;
std::string spouseName;
std::string diagnosis;
};
将每个受保护成员变量的类型从 char *
更改为 std::string
现在将允许您从标准输入中读取字符串并将它们存储在每个成员变量中,并且 std::string
对象将根据需要处理所有必要的内存分配(以及在不再使用时清理它)。然后您应该能够按原样使用您的函数 FemaleIn::enterPatientData
因为语法是正确的。
除此之外,正如其他人所指出的,您可能需要重新考虑您的 class 层次结构设计,但这在这里应该不是问题。您可能还需要重新考虑如何存储某些类型的变量(例如,age
可能更好地存储为 int
)。