可以說我們有一個類層次結構。在底部我們有Base和Derived頂部。 如何確定對象類,即使它被轉換爲基類指針。如何在C++運行時確定實際的對象類型;
Base* b = new Derived():
typeid(b).name(); // i want this to tell me that this is actually derived not base object
除了手動實現字符串字段或這樣的虛擬get函數之外,還有其他任何方法嗎?
PS:我講的編譯器無關的解決方案
這是可能的,正如答案中所解釋的那樣,但這幾乎總是一件錯誤的事情。你想在這裏做什麼? – 2013-04-09 09:28:02
沒什麼,只是爲了爭辯的目的:) – user1079475 2013-04-09 09:40:23
更多面嚮應用程序的版本:http://stackoverflow.com/questions/351845/finding-the-type-of-an-object-in-c ||沒有提及繼承的更通用的版本:http://stackoverflow.com/questions/11310898/how-do-i-get-the-type-of-a-variable – 2015-06-18 15:11:15