相关疑难解决方法(0)

754
推荐指数
22
解决办法
55万
查看次数

我们何时必须使用复制构造函数?

我知道C++编译器为类创建了一个复制构造函数.在这种情况下,我们必须编写用户定义的复制构造函数吗?你能举一些例子吗?

c++ copy-constructor

80
推荐指数
4
解决办法
7万
查看次数

Vector无法正确创建对象

我创建了一个带有一些静态数据的类.像这样的东西:

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)

c++ stdvector

4
推荐指数
1
解决办法
229
查看次数