1

我有一堂课,

class Points
{
 public:
 Points();
}

还有一个,

class OtherPoints : public Points
{
 public:
 OtherPoints ();

 Points myPoints;
}

现在在OtherPoints()构造函数中,我正在尝试创建一个Point变量,例如,

OtherPoints::OtherPoints(){ 
    myPoints=Points();
}

并得到错误,

错误 C2582:“点”中的“运算符 =”功能不可用

4

2 回答 2

2

我认为myPoints=Points();不需要;

Points myPoints; // This code has already called the constructor (Points();)
于 2013-08-27T04:29:14.817 回答
0

这是我编译的代码,它编译得很好,

 #include<iostream>
 using namespace std;
 class points{
    public: points(){cout<<"constructor points called"<<endl;}
            virtual ~points(){cout<<"destructor points called"<<endl;}
 };
 class otherpoints: public points{
                    points x1;
    public: otherpoints(){cout<<"constructor otherpoints called"<<endl;x1=points();}
            ~otherpoints(){cout<<"destructor otherpoints called"<<endl;}
 };
 int main(int argc, char *argv[])
 {
    otherpoints y1=otherpoints();        
    return 0;
 }

输出是,

构造函数点调用

构造函数点调用

构造函数 otherpoints 调用

构造函数点调用

调用的析构函数点

调用其他点的析构函数

调用的析构函数点

调用的析构函数点

我没有收到任何分配错误。

注意:每当您进行继承时,都会将基类析构函数设为虚拟。

于 2013-08-27T11:37:25.977 回答