簡體   English   中英

析構函數不在控制台上打印行

[英]Destructor doesn't print line on console

我正在使用Visual Studio 2013並打印一些cout命令進行測試,當我調試代碼時,該控件進入這兩個類的析構函數內,但未在控制台上打印cout語句。

#include <iostream>  
using namespace std;

class Uni
{
 public:
 ~Uni()
 {
    cout << "I am in Uni Destructor" << endl;
 }

 Uni()
 {
    cout << "I am in Uni Constructor" << endl;
 }

};


class Student: public Uni
{
 public:

 Student()
 {
    cout << "I am in Student Constructor" << endl;
 }
 ~Student()
 {
    cout << "I am in Student Destructor" << endl;
 }



};

int main()
{
  Student s;
  system("pause");
  return 0;
}

輸出:

我在Uni構造函數中

我在學生組織者中

我想您在這一點上(即在pause之后)得到了輸出。

int main()
{
  Student s;
  system("pause");

  // here; but s is not destroyed yet
  return 0;
}

但是到那時s還沒有被破壞。 當超出聲明的范圍時,即離開函數main時,它將被銷毀。

您可以將s放入進一步的測試范圍。

int main()
{
  {
    Student s;
  } // s will be destroyed here

  system("pause");
  return 0;
}

您的代碼看起來不錯,cout包含一個緩沖的控制台輸出流對象實例:您確定析構函數消息ist不僅被緩沖以在cout的最后一個大括號和刷新之后被寫入嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM