简体   繁体   English

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

[英]C++! How are destructors called

so i'm new to C++ (with java as a background) and i was going through copy constructor and destructor section but i still don't get it. 所以我是C ++的新手(以java为背景)我正在通过copy constructordestructor部分,但我仍然没有得到它。 The example i will show you is taken from tutorialpoints.com. 我将向您展示的示例来自tutorialpoints.com。 So i have this code but the output just confuses me. 所以我有这个代码,但输出只是让我感到困惑。

#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;
}

Output 产量

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!

So in line 1 (Main), why does it also call the copy constructor when it has its own argument? 所以在第1行(Main)中,为什么它还有自己的参数时调用复制构造函数?
Also after the first call to the method display why is there another call to the copy constructor ? 在第一次调用方法display为什么还有另一个调用copy constructor
this is the input i would have expected 这是我预期的输入

`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?!` 

Sorry if it was too long, hopefully i was clear. 对不起,如果它太长了,希望我很清楚。 PS i'm Only familiar with regular pointers! PS我只熟悉常规指针!

What's confusing you is that display takes a Line by value. 令你困惑的是, display采用Line by value。 When you pass a Line to the display function, it copies it (calls copy ctor) into its local argument and when the display function ends, that local argument goes out of scope and the copied Line destructs. Line传递给display函数时,它会将其复制(调用copy ctor)到其本地参数中,当display函数结束时,该局部参数超出范围并且复制的Line破坏。

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

The compiler emits code that automatically calls the destructor when an object is explicitly destroyed (eg delete on an object created with operator new ) or reaches the end of its defined life (eg a temporary destroyed when no longer needed, a declared variable passing out of scope). 编译器发出的代码在显式销毁对象时自动调用析构函数(例如delete使用operator new创建的对象)或到达其定义生命的末尾(例如,不再需要时临时销毁,声明的变量传出范围)。

In your code (as you have it at the time of my writing this) the sequence of events is 在你的代码中(正如你在我写这篇文章时那样),事件的顺序是

  • line1 is constructed line1构造
  • line2 is constructed as a copy of line1 line2构造为line1的副本
  • line1 is passed by value to display() . line1通过值传递给display() This creates a temporary copy of line1 . 这将创建line1的临时副本。 When display() returns, the destructor is invoked for the temporary. display()返回时,将为临时调用析构函数。
  • line2 is passed by value to display() . line2通过值传递给display() This creates a temporary copy of line2 . 这将创建line2的临时副本。 When display() returns, the destructor is invoked for the temporary. display()返回时,将为临时调用析构函数。
  • main() returns. main()返回。
  • line1 and line2 are destructed in reverse order of their construction, and destructor invoked for each. line1line2按其构造的相反顺序进行破坏,并为每个析构函数调用析构函数。

So in line 1 (Main), why does it also call the copy constructor when it has its own argument? 所以在第1行(Main)中,为什么它还有自己的参数时调用复制构造函数?

It will not call copy constructor, comment your code in main and try. 它不会调用复制构造函数,在main中注释你的代码并尝试。 live_demo: http://coliru.stacked-crooked.com/a/64ad2d47a7ee953b live_demo: http ://coliru.stacked-crooked.com/a/64ad2d47a7ee953b

Also after the first call to the method display why is there another call to the copy constructor ? 在第一次调用方法显示之后,为什么还有另一个调用复制构造函数?

void display(Line obj)

When you invoke this function since your passing argument by value system creates a local copy of object named obj to function, it is happening using copy constructor of your class. 当您调用此函数时,由于您通过值系统传递参数会创建一个名为obj的对象的本地副本到函数,它将使用您的类的复制构造函数进行。 This local objects scope will end as soon execution completes the scope of your function display . 一旦执行完成功能display的范围,此本地对象范围将结束。

Here is each part of main along with what it prints. 这是主要的每个部分以及它的打印内容。

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

Normal constructor allocating ptr

Line line2 = line1; line line2 = line1; // This also calls copy constructor //这也调用了复制构造函数

Copy constructor allocating ptr.

display(line1); 显示器(LINE1);

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

display(line2); 显示器(LINE2);

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

return 0; 返回0; } }

Freeing memory!
Freeing memory!

You are passing in Line by value so it copies the Line you have, calls the function and then the end of that function deletes the Line it got. 您正在按Line传递值,因此它会复制您拥有的Line ,调用该函数,然后该函数的结尾将删除它所获得的Line

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM