了解对象何时被销毁

时间:2014-07-22 16:45:09

标签: c++ destructor

我在this的答案中被告知要删除析构函数中的成员myValue。但如果我添加它我会得到一个例外。从控制台消息中,它似乎是在两个时刻使用相同的对象进入析构函数(!?)

注意:代码是我尝试练习。在其中,我们将获得一个代码,我们只能向其添加。本质上,main和没有参数的构造函数的内容是给出的。剩下的就是我要添加它以使其工作。

我的猜测是,在某个函数调用期间创建的对象的副本在退出函数调用后会破坏相同的myValue。在这种情况下,如何正确保存两个对象指向的数据?

// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

class MyFancyInt
{
public:
    // A constructor that inputs a value for initialization, so that line /*1*/ makes sense.
    MyFancyInt(int x) : myValue(new int(x))// The part after the : initializes myValue to point to the newly created int with value x.
    {
        cout << "Constructor that initializes with myValue = " << x << endl;
    };

    //Destructor.
    ~MyFancyInt()
    {  
        cout << "A MyFancyInt destroyed that had the myValue = "<< this->myValue<< " pointing to a "<< *(this->myValue) << endl;
        //delete myValue;
    };

    //A 'Copy Constructor', so that line /*3*/ makes sense. Aparently C++ creates default copy constructors.
    //A copy constructor input an already existing MyFancyInt, creates a new MyFancyInt and initializes it with the data of the former.
    MyFancyInt(MyFancyInt& x)
    {
        myValue = x.myValue;
    };

    //The construtor without parameters already given in the exercise.
    MyFancyInt()
    {
        cout << "Default constructor" << endl;
        myValue = 0;
    }

    // Friend function to overload the operator +. This one takes two MyFancyInt. It is not needed. It can be commented out.
    friend MyFancyInt operator+(MyFancyInt &x, MyFancyInt &y);

    // Friend function to overload the operator +. This one takes an int and a MyFancyInt. It is needed for line /*6*/.
    friend MyFancyInt operator+(int x, MyFancyInt &y);

    // Friend function to overload the operator +. This one takes a MyFancyInt and an int. It is needed for line /*5*/.
    friend MyFancyInt operator+(MyFancyInt &x, int y);

    // Friend function to overload the output stream operator <<. It is needed for the printing in line /*7*/.
    friend ostream& operator<<(ostream& os, const MyFancyInt& x);

    // Overloading the operator =. It is needed for line /*2*/, /*5*/, and /*6*/.
    MyFancyInt& operator=(const MyFancyInt &x);
private:
    int* myValue;

};

MyFancyInt operator+(MyFancyInt &x, MyFancyInt &y)
{
    cout << "MyFancyInt + MyFancyInt = " << x.myValue << " + " << y.myValue << "= " << *(x.myValue) << " + " << *(y.myValue) << endl;
    MyFancyInt z=MyFancyInt(*(x.myValue) + *(y.myValue));
    cout << "= " << *(z.myValue) << endl;
    return z;
};
 MyFancyInt operator+(int x, MyFancyInt &y)
 {
     cout << "int + MyFancyInt = " << x << " + " << y.myValue << " = " << x << " + " << *(y.myValue)<< endl;
     MyFancyInt z = MyFancyInt(x + *(y.myValue));
     cout << " = " << *(z.myValue) << endl;
     return z;
 };
 MyFancyInt operator+(MyFancyInt &x, int y)
 {
     cout << "MyFancyInt + int = " << x.myValue << " + " << y << " = " << *(x.myValue) << " + " << y << endl;
     MyFancyInt z = MyFancyInt(*(x.myValue) + y);
     cout << " = " << *(z.myValue) << endl;
     return z;
 };

 ostream& operator<<(ostream& os, const MyFancyInt& x)
 {
     os << *(x.myValue);
     return os;
 }

 MyFancyInt& MyFancyInt::operator=(const MyFancyInt &x)
 {
     cout << "Entering the assigment operator." << endl;
     myValue = x.myValue;
     return *this;
 };

int _tmain(int argc, _TCHAR* argv[])
{
    MyFancyInt mfi1(1);     /*1*/
    MyFancyInt mfi2 = mfi1; /*2*/
    MyFancyInt mfi3(mfi1);  /*3*/
    MyFancyInt mfi4;        /*4*/
    mfi4 = mfi3 + 2;        /*5*/
    mfi4 = 3 + mfi3;        /*6*/
    cout << mfi4 << endl;   /*7*/
    return 0;
}

1 个答案:

答案 0 :(得分:1)

复制构造函数的编写方式,复制对象时,两个对象都将指向同一个地址,并且都会尝试删除它。