2013-01-08 120 views
1

內FileTwo.h顯示對象名稱

#include"iostream" 
    using namespace std ; 
    class FileTwo{ 
    public: 
     FileTwo(){ 
     cout<<"constructor for";//Here want to show the object for which the constructor has been called 
     } 
    ~Filetwo(){ 
     cout<<"Destructor for ";//Here want to show the object for which the destructor has been called 
    }; 

裏面的main.cpp

#include"Filetwo.h" 

int main(){ 
    FileTwo two ; 
    return 0; 
} 

我知道這個示例程序是非常小的,所以我們可以能夠找出哪些對象構造函數和析構函數已被調用。但對於大項目,有什麼方法可以知道對象名稱?提前致謝 。

+1

你是什麼意思的「名稱」?變量的標識符?像'foo(FileTwo());'這樣的代碼中的「名稱」是什麼? – Angew

+0

你不能。C++不支持反射。 – zapredelom

+1

@zapredelom反射與它無關。對象在C++中沒有名稱,句號。沒有任何反射會解決這個問題。另一方面,C++確實有RTTI,它將提供類型信息。 – EJP

回答

3

這是可能的。如果你的編譯支持__PRETTY_FUNCTION____func__(見this),那麼你可以這樣做:

#include <iostream> 
using namespace std; 
class FileTwo{ 
    public: 
    FileTwo(){ 
     cerr<<"constructor for "<< __PRETTY_FUNCTION__ <<" at "<<&(*this)<<endl; 
    } 
    ~FileTwo(){ 
     cerr<<"Destructor for "<< __PRETTY_FUNCTION__ <<" at "<<&(*this)<<endl; 
    } 
}; 

int main(){ 
    FileTwo two; 
    return 0; 
} 

請注意,我也印到cerr以確保此輸出被立即刷新,如果程序不丟失崩潰。另外,由於每個對象都有唯一的指針,因此我們可以使用它來查看特定對象何時被製造或被殺死。

用於我的計算機上的上述程序的輸出是:

constructor for FileTwo::FileTwo() at 0x7fff641cde40 
Destructor for FileTwo::FileTwo() at 0x7fff641cde40 

注意__func__是C99標準標識符。 C++ 0x以「實現定義的字符串」的形式添加了支持。

__FUNCTION__是某些編譯器(包括Visual C++(請參閱documentation)和gcc(請參閱documentation))支持的預標準擴展。

__PRETTY_FUNCION__是一個海灣合作委員會的擴展,它做同樣的東西,但更漂亮。

This question有關於這些標識符的更多信息。

根據您的編譯器,這可能會返回類的名稱,儘管它可能有點損壞。

#include <iostream> 
#include <typeinfo> 

using namespace std; 
class FileTwo{ 
    public: 
    FileTwo(){ 
     cerr<<"constructor for "<< typeid(*this).name() <<" at "<<&(*this)<<endl; 
    } 
    ~FileTwo(){ 
     cerr<<"Destructor for "<< typeid(*this).name() <<" at "<<&(*this)<<endl; 
    } 
}; 

int main(){ 
    FileTwo two; 
    return 0; 
} 

如果你試圖去哪個類被實例變量的名稱(two你的情況),那麼就沒有,就我所知,一個辦法做到這一點。以下將模擬它:

#include <iostream> 
#include <string> 

using namespace std; 
class FileTwo{ 
    public: 
    FileTwo(const std::string &myName) : myName(myName) { 
     cerr<<"constructor for "<< myName <<" at "<<&(*this)<<endl; 
    } 
    ~FileTwo(){ 
     cerr<<"Destructor for "<< myName <<" at "<<&(*this)<<endl; 
    } 
    private: 
    std::string myName; 
}; 

int main(){ 
    FileTwo two("two"); 
    return 0; 
} 
+1

+1,這真棒:-) – abnvp

+0

不錯的一個。因爲它重定向我以得到我的答案。 – Kenta

+0

你的回答最終成了什麼,@Learner? – Richard

4

除非您命名對象,否則不可能。事情是這樣的:

#include <iostream> 
#include <string> 
using namespace std; 
class FileTwo { 
    public: 
    FileTwo(const std::string &myName) : name(myName){ 
     cout<<"constructor for" << name;//Here want to show the object for which the constructor has been called 
    } 
    ~Filetwo(){ 
     cout<<"Destructor for " << name;//Here want to show the object for which the destructor has been called 
    } 

    private: 
    std::string name; 
}; 

,然後改變主要爲:

#include"Filetwo.h" 
int main(){ 
    FileTwo two("two 11"); 
} 
1

這是不可能的命名對象,所有你能做的是什麼使一個私有變量來保存名稱。

using namespace std; 
class myClass 
{ 
    private: 
    string className; 

    public: 
    ~myClass() 
    { 
     cout<<this->className; 
    } 
}; 

你可以爲你創建setter和getters變量。

void SetName(string name) 
{ 
    this->className = name; 
} 

string GetName() 
{ 
    return this->className; 
} 
相關問題