我知道C++编译器为类创建了一个复制构造函数.在这种情况下,我们必须编写用户定义的复制构造函数吗?你能举一些例子吗?
我创建了一个带有一些静态数据的类.像这样的东西:
class Object
{
public:
Object();
Object( Point center, double area );
Object( int cx, int cy, double area );
~Object();
//and other public stuffs here...
private:
Point center;
double area;
static double totalArea;
static int objCounter;
static double areaMean;
};
Run Code Online (Sandbox Code Playgroud)
而不是在我的构造函数和析构函数中:
Object::Object()
{
this->setCenter( Point() );
this->setArea( 0.0 );
objCounter++;
totalArea += 0;
areaMean = totalArea / objCounter;
}
/*this is just the default constructor I
have two others that increment real area, not 0*/
Object::~Object()
{
//cout << …Run Code Online (Sandbox Code Playgroud)