我无法弄清楚我的“副本”变量出了什么问题。
这是我家庭作业的演示代码,向我们展示了基本语法以及如何使用不同类型的OOP构造函数和关键字。问题是什么时候运行,一切运行良好,但问题出在copys变量中,在复制构造函数时,它的值应该为1,但似乎不起作用。
#include <iostream>
#include <string>
using namespace std;
const int JANE_HOURS = 30, JIM_HOURS = 20, SETTER_HOURS = 40;
const double SETTER_DAYS = 3.0, HOURS_PER_DAY = 24.0;
class WorkerHours {
private:
int hoursWorked;
int copies;
public:
void setData(int);
void setData(double);
int getCpy();
WorkerHours();
WorkerHours(int);
WorkerHours operator + (const WorkerHours &combinedOb) const;
WorkerHours operator += (const WorkerHours &combinedOb);
operator int() const;
operator double() const;
friend void showInternalData(string label, const WorkerHours &worker);
WorkerHours(WorkerHours &janeCpy);
~WorkerHours();
};
WorkerHours::WorkerHours() {
hoursWorked = 0;
copies = 0;
}
WorkerHours::WorkerHours(int hoursWork) {
hoursWorked = hoursWork;
}
WorkerHours::WorkerHours(WorkerHours &janeCpy) {
hoursWorked = janeCpy.hoursWorked;
copies = (janeCpy.copies + 1);
}
int WorkerHours::getCpy() {
return copies;
}
WorkerHours WorkerHours::operator += (const WorkerHours &combinedOb) {
WorkerHours result;
hoursWorked += combinedOb.hoursWorked;
return result;
}
WorkerHours WorkerHours::operator + (const WorkerHours &combinedOb) const {
WorkerHours result;
result.hoursWorked = hoursWorked + combinedOb.hoursWorked;
return result;
}
void WorkerHours::setData(int a) {
hoursWorked = a;
}
void WorkerHours::setData(double a) {
hoursWorked = a * HOURS_PER_DAY;
}
WorkerHours::operator int() const {
return hoursWorked;
}
WorkerHours::operator double() const {
return (hoursWorked / HOURS_PER_DAY);
}
WorkerHours::~WorkerHours() {
cout << "Destroyed" << endl;
}
// This is the prototype of the showInternalData function.
// It must access INTERNAL STRUCTURES in the worker object.
// Do NOT use member functions to get the data for that object.
void showInternalData(string label, WorkerHours &worker);
int main()
{
// Conversion constructor
WorkerHours jane = JANE_HOURS, jim = JIM_HOURS;
// Copy constructor
WorkerHours janeCopy = jane;
// + operator
WorkerHours combined = jane + jim;
// Default constructor
WorkerHours testSetters;
// Variables set aside for calculations
double daysWorked;
int hoursWorked;
// You can use static_cast here, but it shouldn't be required.
//static_cast<double>(combined);
// Type conversion operator - int
daysWorked = combined;
cout << "TEST DAYS WORKED : " << daysWorked << endl;
// You can use static_cast here, but it shouldn't be required.
// static_cast<int>(combined);
// Type conversion operator - double
hoursWorked = combined; //using the = already (over loading)
cout << "TEST HOURS WORKED: " << hoursWorked << endl;
// Now we start using the internal function
showInternalData("Jane", jane);
showInternalData("JaneCopy", janeCopy);
showInternalData("Jim", jim);
showInternalData("Combined", combined);
// += operators
jane += janeCopy;
showInternalData("Jane + JaneCopy", jane);
// Now we test the overloaded setters
testSetters.setData(SETTER_HOURS);
showInternalData("Testing int setter", testSetters);
testSetters.setData(SETTER_DAYS);
showInternalData("Testing double setter", testSetters);
// We’re done
system("pause");
return 0;
}
void showInternalData(string label, WorkerHours &worker) {
string x = label;
cout << "Data: " << x << ", " << "Hours worked:" << worker.operator int() << ", " << "Copy generation: "<< worker.getCpy()<< endl;
}
输出结果如下:
TEST DAYS WORKED : 2.08333
TEST HOURS WORKED: 50
Data: Jane, Hours worked: 30, Copy generation: 0
Data: JaneCopy, Hours worked: 30, Copy generation: 1
Data: Jim, Hours worked: 20, Copy generation: 0
Data: Combined, Hours worked: 50, Copy generation: 0
Data: Jane + JaneCopy, Hours worked: 60, Copy generation: 0
Data: Testing int setter, Hours worked: 40, Copy generation: 0
Data: Testing double setter, Hours worked: 72, Copy generation: 0
答案 0 :(得分:0)
问题很简单。您的代码工作正常。它是您正在使用的编译器。在这种情况下,请勿使用Visuals c ++。 Visual C ++的处理方式有所不同。它与GCC一样使用重载的+运算符,但是此后,它将+运算符功能产生的对象放入COPY CONSTRUCTOR中,然后在产生的对象上运行。复制构造函数的结果,而不是重载的+的结果,最终出现在名为“ combined”的变量中。