C ++!如何调用析构函数

时间:2015-06-25 00:38:31

标签: c++ pointers

所以我是C ++的新手(以java为背景)我正在浏览copy constructordestructor部分,但我仍然没有得到它。我将向您展示的示例来自tutorialpoints.com。所以我有这个代码,但输出只是让我感到困惑。

#include <iostream>

using namespace std;

class Line
{
   public:
      int getLength( void );
      Line( int len );             // simple constructor
      Line( const Line &obj);  // copy constructor
      ~Line();                     // destructor

   private:
      int *ptr;
};

// Member functions definitions including constructor
Line::Line(int len)
{
    cout << "Normal constructor allocating ptr" << endl;
    // allocate memory for the pointer;
    ptr = new int;
    *ptr = len;
}

Line::Line(const Line &obj)
{
    cout << "Copy constructor allocating ptr." << endl;
    ptr = new int;
   *ptr = *obj.ptr; // copy the value
}

Line::~Line(void)
{
    cout << "Freeing memory!" << endl;
    delete ptr;
}
int Line::getLength( void )
{
    return *ptr;
}

void display(Line obj)
{
   cout << "Length of line : " << obj.getLength() <<endl;
}

// Main function for the program
int main( )
{
   Line line1(10);  //Line 1***************************

   Line line2 = line1; // This also calls copy constructor

   display(line1);
   display(line2);

   return 0;
}

输出

Normal constructor allocating ptr
Copy constructor allocating ptr.
Copy constructor allocating ptr.
Length of line : 10
Freeing memory!
Copy constructor allocating ptr.
Length of line : 10
Freeing memory!
Freeing memory!
Freeing memory!

所以在第1行(Main)中,为什么在它有自己的参数时也会调用复制构造函数? 在第一次调用方法display之后,为什么还有另一个调用copy constructor? 这是我预期的输入

`Normal constructor allocating ptr //Because Line one has its argument
Copy constructor allocating ptr.  //Because Line2 is a copy of Line1
Length of line : 10
Freeing memory!  // Call to destructor after Line1 is desplayed
// Memory leak Since Line2 is now pointing to nothing, i guess is what we call `shallow pointers?!` 

对不起,如果时间太长,希望我很清楚。 PS我只熟悉常规指针!

4 个答案:

答案 0 :(得分:8)

令您感到困惑的是,display按值Line获取。当您将Line传递给display函数时,它会将其复制(调用copy ctor)到其本地参数中,当display函数结束时,该局部参数超出范围并且复制了Line destructs。

int main( )
{
   Line line1(10);  // calls ctor that takes an int

   Line line2 = line1; // calls copy ctor

   display(line1); // calls copy ctor, prints, and then destructs the copy
   display(line2); // calls copy ctor, prints, and then destructs the copy

   return 0;
} // line 2 goes out of scope and destructs, then line 1 does the same

答案 1 :(得分:1)

编译器发出的代码在显式销毁对象时自动调用析构函数(例如,在使用运算符delete创建的对象上的new)或到达其定义生命的末尾(例如,临时销毁)当不再需要时,声明的变量超出范围)。

在你的代码中(正如你在我写这篇文章时那样),事件的顺序是

    构建
  • line1
  • line2构建为line1
  • 的副本
  • line1按值传递给display()。这创造了一个临时的 line1的副本。当display()返回时,将为临时函数调用析构函数。
  • line2按值传递给display()。这创造了一个临时的 line2的副本。当display()返回时,将调用析构函数 为临时。
  • main()返回。
  • line1line2按其相反顺序销毁 为每个人调用构造和析构函数。

答案 2 :(得分:0)

  

所以在第1行(Main)中,为什么还要调用复制构造函数   它有自己的论点吗?

它不会调用复制构造函数,在main中注释你的代码并尝试。 live_demo: http://coliru.stacked-crooked.com/a/64ad2d47a7ee953b

  

同样在第一次调用方法后显示为什么还有另一个   调用复制构造函数?

void display(Line obj)

当你调用这个函数时,因为你通过值系统传递的参数创建了一个名为obj的对象的本地副本来运行,它正在使用你的类的复制构造函数。执行完成函数display的范围后,此本地对象范围将结束。

答案 3 :(得分:0)

这是主要的每个部分以及它的打印内容。

第1行(10); //第1行***************************

Normal constructor allocating ptr

第2行= line1; //这也称为复制构造函数

Copy constructor allocating ptr.

显示器(LINE1);

Copy constructor allocating ptr.
Length of line : 10
Freeing memory!

显示器(LINE2);

Copy constructor allocating ptr.
Length of line : 10
Freeing memory!

返回0; }

Freeing memory!
Freeing memory!

您按价值传递Line,因此它会复制您拥有的Line,调用该函数,然后该函数的结尾会删除它所获得的Line