C++ 如何编写用于合成的复制构造函数?
下面是我的代码:C++ 如何编写用于合成的复制构造函数?,c++,copy-constructor,composition,C++,Copy Constructor,Composition,下面是我的代码: class Pulley{ private: int noOfTeeth; public: Pulley(); Pulley(int teeth); ~Pulley(); void show(); }; Pulley::Pulley() { cout << "Pulley constructor called!"; noOfTeeth = 0; } Pulley::Pulley(int teeth) {
class Pulley{
private:
int noOfTeeth;
public:
Pulley();
Pulley(int teeth);
~Pulley();
void show();
};
Pulley::Pulley()
{
cout << "Pulley constructor called!";
noOfTeeth = 0;
}
Pulley::Pulley(int teeth)
{
cout << "Pulley constructor called!";
noOfTeeth = teeth;
}
void Pulley::show()
{
cout << "\n\nNo of Teeths of Pulley: " << noOfTeeth;
}
Pulley::~Pulley()
{
}
class GearBox{
private:
char *transmission;
Pulley p;
public:
GearBox();
GearBox(char *trans, int pTeeth);
~GearBox();
void show();
};
GearBox::GearBox(): p()
{
cout << "Gearbox constructor called!";
transmission = NULL;
}
GearBox::GearBox(char *trans, int pTeeth): p(pTeeth)
{
cout << "Gearbox constructor called!";
if(trans != NULL)
{
transmission = new char[strlen(trans)+1];
strcpy(transmission, trans);
}
else
{
transmission = NULL;
}
}
void GearBox::show()
{
cout << "\n\nTransmission of vehicle: " << transmission;
p.show();
}
GearBox::~GearBox()
{
}
class Vehicle{
private:
char *model;
char *color;
GearBox g;
public:
Vehicle();
Vehicle(char *mod, char *col, char *gr);
Vehicle(const Vehicle &vh);
~Vehicle();
void show();
};
Vehicle::Vehicle(): g()
{
cout << "Vehicle constructor called!";
model = NULL;
color = NULL;
}
Vehicle::Vehicle(char *mod, char *col, char *gr): g(gr)
{
cout << "Vehicle constructor called!";
if(mod != NULL)
{
model = new char[strlen(mod)+1];
strcpy(model, mod);
}
else
{
model = NULL;
}
if(col != NULL)
{
color = new char[strlen(col)+1];
strcpy(color, col);
}
else
{
color = NULL;
}
}
void Vehicle::show()
{
cout << "\n\nModel of Vehicle: " << model;
cout << "\n\nColor of Vehicle: " << color;
g.show();
}
int main()
{
Pulley p(20);
GearBox g("Manual", p);
Vehicle V("Honda", "Black", g);
V.show();
system("PAUSE");
}
现在,当我运行这段代码时,我得到了很多错误,我不知道这些错误是什么,以及如何解决它们。我理解的一个错误是复制构造函数,所以有谁能解释我如何编写用于字符传输、模型和颜色指针的复制构造函数。还请告诉我如何解决其他错误。非常感谢。试试这样:
#include <string>
#include <iostream>
class Pulley{
private:
int noOfTeeth;
public:
Pulley(int teeth = 0);
void show();
};
Pulley::Pulley(int teeth)
: noOfTeeth(teeth)
{
std::cout << "Pulley constructor called!";
}
void Pulley::show()
{
std::cout << "\n\nNo of Teeths of Pulley: " << noOfTeeth;
}
class GearBox{
private:
std::string transmission;
Pulley p;
public:
GearBox(std::string trans = std::string(), Pulley p = Pulley());
void show();
};
GearBox::GearBox(std::string trans, Pulley p)
: transmission(std::move(trans))
, p(std::move(p))
{
std::cout << "Gearbox constructor called!";
}
void GearBox::show()
{
std::cout << "\n\nTransmission of vehicle: " << transmission;
p.show();
}
class Vehicle{
private:
std::string model;
std::string color;
GearBox g;
public:
Vehicle();
Vehicle(std::string mod = "", std::string col = "", GearBox gr = GearBox());
void show();
};
Vehicle::Vehicle(std::string mod,std::string col, GearBox gr)
: model(std::move(mod))
, color(std::move(col))
, g(std::move(gr))
{
std::cout << "Vehicle constructor called!";
}
void Vehicle::show()
{
std::cout << "\n\nModel of Vehicle: " << model;
std::cout << "\n\nColor of Vehicle: " << color;
g.show();
}
int main()
{
Pulley p(20);
GearBox g("Manual", p);
Vehicle V("Honda", "Black", g);
V.show();
// system("PAUSE");
}
在编写代码时,先从一些小而简单、工作完美的东西开始,然后一次增加一点复杂性。如果您在这么多代码中有很多错误,那是因为您没有在出现错误时解决它们。永远不要添加不起作用的代码。我建议您尽可能简化此代码,同时仍会出现一些错误;您可能会发现错误变得很明显,否则您可以发布一个更简单的示例。存储字符串时,请使用std::string。不要写析构函数或复制运算符。委托给std::string、std::vector、std::unique_ptr等。现在,当我运行这段代码时,我得到了很多错误,我不知道这些错误是什么以及如何解决它们。从这句话中,我们不清楚您是否有编译错误,如果是,这些错误是什么?或者您的代码在运行时失败了。如果是,您是否尝试使用调试器单步检查代码,以找出失败的原因?在实现构造函数、析构函数和赋值运算符时,理解这一点非常重要。遵循这个建议可能会解决你的问题。为什么你调用主体中的构造函数来调用与调用不匹配的构造函数?我会认为这个答案不完整。它既没有说明做了什么改变,也没有解释改变背后的原因。你能在不教我任何东西的情况下帮我做家庭作业吗?它是在一篇文章的基础上发布的comment@RichardHodges你能解释一下movetrans和movep的代码吗,首先是移动一个内置函数,如果是的话,我们用它来做什么?@ KuBaBikHaar关于C++标准库函数的所有问题都回答如下: