[問題] VC的constructor 呼叫了 destructor?
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
VC++
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
無
問題(Question):
在Programming版問了如何trace code的開始與結束的時候
版上大大提供
宣告一個物件 建構子的時候印出開始符號 解構子的時候印出結束符號
進function就宣告此物件
在BCB測試順利
在VC下 建構子一呼叫完解構子就被呼叫
預期的正確結果(Expected Output):
line1 (表示開始)
line2 (表示function內容)
line3 (表示結束)
錯誤結果(Wrong Output):
line1
line3
line2
line3
程式碼(Code):(請善用置底文網頁, 記得排版)
.h
#include <iostream>
using namespace std;
struct BEGINEND
{
public:
BEGINEND(char* file, int line, wstring str);
~BEGINEND(void);
private:
char *file_;
int line_;
wstring str_;
};
=========================================================
.cpp
#include "stdafx.h"
#include "test.h"
#include <iostream>
using namespace std;
BEGINEND::BEGINEND(char* file, int line, wstring str)
{
void* ptr;
ptr = this;
file_ = file;
line_ = line;
str_ = str;
{
cout << "line1" << endl;
}
}
BEGINEND::~BEGINEND(void)
{
void* ptr;
ptr = this;
cout << "line3" << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
BEGINEND be = BEGINEND(__FILE__, __LINE__, L"haha");
cout << "line2" << endl;
return 0;
}
補充說明(Supplement):
錯誤輸出的情況我有試著把this印出
結果
line1
line3
lien2
line3
紅色兩行的this與綠色行的this不同個
想請教一下這情況是哪邊出問題了呢
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 210.59.147.226
※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1411372385.A.CA8.html
推
09/22 16:02, , 1F
09/22 16:02, 1F
→
09/22 16:02, , 2F
09/22 16:02, 2F
→
09/22 16:03, , 3F
09/22 16:03, 3F
→
09/22 16:03, , 4F
09/22 16:03, 4F
→
09/22 16:18, , 5F
09/22 16:18, 5F
→
09/22 17:02, , 6F
09/22 17:02, 6F
→
09/22 17:09, , 7F
09/22 17:09, 7F
→
09/22 17:09, , 8F
09/22 17:09, 8F
→
09/22 17:10, , 9F
09/22 17:10, 9F
→
09/22 17:14, , 10F
09/22 17:14, 10F
→
09/22 17:14, , 11F
09/22 17:14, 11F
→
09/22 17:15, , 12F
09/22 17:15, 12F
→
09/22 17:32, , 13F
09/22 17:32, 13F
→
09/22 18:43, , 14F
09/22 18:43, 14F
推
09/22 19:33, , 15F
09/22 19:33, 15F
→
09/22 19:34, , 16F
09/22 19:34, 16F
→
09/22 19:34, , 17F
09/22 19:34, 17F
→
09/22 19:35, , 18F
09/22 19:35, 18F
→
09/23 11:29, , 19F
09/23 11:29, 19F