c++ - 类构造函数不保存变量

标签 c++ class oop this

当尝试使用 getter 获取我的 Employee 类中变量的值时,没有任何返回或输出。

我尝试过使用 setter,这不会做太多,因为他们仍然使用 this-> 方法,但没有达到我设置的值。

class Employee {
 Employee(int empNum, std::string name, std::string address, std::string 
phone);

private: 
 int empNum;
 std::string name;
 std::string address;
 std::string phone;
};

class HourlyEmployee : public Employee {
 HourlyEmployee(int empNum, std::string name, std::string address, 
std::string phone, double hourlyWage, double hoursWorked);

//getters
 double getHoursWorked();
 double getHourlyWage();

//setters
 void setHoursWorked(double hoursWorked);
 void setHourlyWage(double hourlyWage);

private:
 double hoursWorked;
 double hourlyWage;
}

//CPP file
Employee::Employee(int empNum, std::string name, std::string address, 
std::string phone) {
 this->empNum = empNum;
 this->name = name;
 this->address = address;
 this->phone = phone;
}
HourlyEmployee::HourlyEmployee(int empNum, std::string name, std::string 
address, std::string phone, double hoursWorked, double hourlyWage) {
 Employee(empNum, name, address, phone);
 this->hoursWorked = hoursWorked;
 this->hourlyWage = hourlyWage;
}

//main
HourlyEmployee hourly1(1, "H. Potter", "Privet Drive", "201-9090", 12.00, 
40.00);
cout << hourly1.getPhone() << " " << hourly1.getName() << " " << 
hourly1.getHoursWorked();

这不是完整的代码,但它应该输出电话和姓名以及工作时间,但由于某种原因它只输出两个空格,然后输出工作时间。我只能假设名称、电话等变量实际上并未设置,因此它们不会返回任何内容。那么我如何让它设置这些变量呢?

最佳答案

C++ 不会专门处理构造函数主体中的构造函数调用。下面一行

Employee(empNum, name, address, phone);

将构建一个新的、完全独立的 Employee 实例,然后丢弃它,因为生成的对象未分配给任何对象。该对象字段的值不会复制到您正在创建的 HourlyEmployee 实例中。

要在子类中使用父类(super class)构造函数,请使用 member initializer list在子类构造函数的定义中。

HourlyEmployee::HourlyEmployee(int empNum, std::string name, std::string 
address, std::string phone, double hoursWorked, double hourlyWage)
        : Employee(empNum, name, address, phone) {
    this->hoursWorked = hoursWorked;
    this->hourlyWage = hourlyWage;
}

这样,C++ 将调用 Employee 的构造函数来初始化 HourlyEmployee 的构造函数正在初始化的同一对象。

您甚至可以走得更远,将您的构造函数缩减为仅包含成员初始化列表。一个很好的副作用是,这避免了必须键入 this-> 或想出不同的命名方案。

Employee::Employee(int empNum, std::string name, std::string address, 
std::string phone) 
        : empNum(empNum),
          name(name),
          address(address),
          phone(phone) {}

HourlyEmployee::HourlyEmployee(int empNum, std::string name, std::string 
address, std::string phone, double hoursWorked, double hourlyWage)
        : Employee(empNum, name, address, phone),
          hoursWorked(hoursWorked),
          hourlyWage(hourlyWage) {}

您可以从上面的链接了解更多信息,但引用一小段解释:

Before the compound statement that forms the function body of the constructor begins executing, initialization of all direct bases, virtual bases, and non-static data members is finished. Member initializer list is the place where non-default initialization of these objects can be specified.

关于c++ - 类构造函数不保存变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55669433/

相关文章:

c++ - C++程序中的选项

java - 我的 getter 返回错误值

java - "java.lang.ArrayIndexOutOfBoundsException"数组逻辑问题

c++ - 使用 delete 调用析构函数

c++ - 虚函数的开销会随着继承树中类的数量增加而增加吗?

java - 如何对这个类进行类型转换?

c# - 我们可以重载重写的方法吗?

C# : assign data to properties via constructor vs. 实例化

c++ - 对于什么 T `std::declval<T>()` 没有匹配函数?

android - 将 UI 控件从 Activity 传递到类