C ++对象被破坏而没有被创建?

时间:2014-07-02 23:47:37

标签: c++ constructor destructor

我尝试了以下代码(从learncpp.com修改)

#include <iostream>
#include <string>

using namespace std;

class Point2D
{
private:
int m_nX;
int m_nY;

public:
// A default constructor
Point2D()
    : m_nX(0), m_nY(0)
{
}

// A specific constructor
Point2D(int nX, int nY)
    : m_nX(nX), m_nY(nY)
{
  cout<<"Point is created"<<endl;
}

~Point2D(){cout<<"Point is destroyed"<<endl;}

// An overloaded output operator
friend std::ostream& operator<<(std::ostream& out, const Point2D &cPoint)
{
    out << "(" << cPoint.GetX() << ", " << cPoint.GetY() << ")";
    return out;
}

// Access functions
void SetPoint(int nX, int nY)
{
    m_nX = nX;
    m_nY = nY;
}

int GetX() const { return m_nX; }
int GetY() const { return m_nY; }
};

class Creature
{
private:
std::string m_strName;
Point2D m_cLocation;

// We don't want people to create Creatures with no name or location
// so our default constructor is private
Creature() { }

public:
Creature(std::string strName, const Point2D &cLocation)
    : m_strName(strName), m_cLocation(cLocation)
{
  cout<<"Creature is created"<<endl;
}

~Creature(){cout<<"Creature is destroyed"<<endl;}

friend std::ostream& operator<<(std::ostream& out, const Creature &cCreature)
{
    out << cCreature.m_strName.c_str() << " is at " << cCreature.m_cLocation;
    return out;
}

void MoveTo(int nX, int nY)
{
    m_cLocation.SetPoint(nX, nY);
}
};


int main()
{
using namespace std;
cout << "Enter a name for your creature: ";
std::string cName;
cin >> cName;
Creature cCreature(cName, Point2D(4, 7));

while (1)
{
    cout << cCreature << endl;
    cout << "Enter new X location for creature (-1 to quit): ";
    int nX=0;
    cin >> nX;
    if (nX == -1)
        break;

    cout << "Enter new Y location for creature (-1 to quit): ";
    int nY=0;
    cin >> nY;
    if (nY == -1)
        break;

    cCreature.MoveTo(nX, nY);
    }

return 0;
}

当我运行程序时,我得到以下内容:

Enter a name for your creature: gabar
Point is created
Creature is created
Point is destroyed
gabar is at: (4,7)

Enter new X location for creature (-1 to quit): 2
Enter new Y location for creature (-1 to quit): 3
gabar is at: (2,3)

Enter new X location for creature (-1 to quit): -1
Creature is destroyed
Point is destroyed

我有两个问题:

为什么第一个“Point is destroyed”被称为? 当只有一个“Point is created”提示时,为什么有两个提示“Point is destroyed”。

感谢

3 个答案:

答案 0 :(得分:5)

我不知道你用什么来跟踪构造函数和析构函数,但我怀疑它没有检测到复制构造函数。

此行创建第​​一个Point2D

Creature cCreature(cName, Point2D(4, 7));

Creature的构造函数通过引用获取它,但随后将其分配给m_cLocation,复制它(调用{I}}复制构造函数,我怀疑它不会被跟踪)。

然后销毁第一个Point2D,它是瞬态的,从未存储过。 Point2D在最后被销毁。

答案 1 :(得分:2)

在本声明中

Creature cCreature(cName, Point2D(4, 7));

创建临时对象Point2D(4, 7)作为第二个参数。完成语句后,此对象将被删除。

在你的程序中有两个Point类型的对象。第一个是上面提到的临时对象,第二个对象是Creature类的成员。通过构造函数cCreature将临时对象复制到成员对象后,它被删除了。

答案 2 :(得分:1)

因为你只是用你的普通构造函数构造一个Point2D,这在你的main方法中会发生。

第二个Point2D,它是成员变量m_cLocation,它不是通过copy constructor构建的,而是由rvalue初始化的,在您的情况下由编译器隐含地定义,而不是由您跟踪控制台打印。此构造函数具有签名Point2D (const Point2D& other)

在用于初始化成员变量之后,第一个点被摧毁,因为它是{{3}}并且在使用后不再需要它。