任何人都可以告訴什麼是動態鑄造手段在c + +。 我們到底在哪裏可以使用這種動態投射? 這是在採訪中問我的,我對這個問題空着:)。究竟是什麼動態鑄造在c + +
回答
嘗試使用搜索第一 old answer
好的指針,雖然跳過接受的答案,並鑽研到第二個(通過litb)這是正確的...不像第一個:/ – 2009-11-23 08:37:32
@Mathieu:時間確定了,litb'現在是第一個答案。無論如何感謝提示。 – philant 2010-09-27 10:18:40
Dynamic casting正在運行時安全地發現對象實例的類型。
這是通過編譯器生成引用表來實現的,引用表可能相當大。出於這個原因,如果程序員知道他們沒有使用該功能,那麼在編譯過程中通常會禁用它。
dynamic_cast的是鑄造方法在運行時發現對象的類。
class Base
{
public:
virtual bool func1();
};
class Derived1 : Base
{
public:
virtual bool func1();
virtual bool funcDer1();
};
class Derived2 : Base
{
public:
virtual bool func1();
virtual bool funcDer2();
};
Base* pDer1 = new Derived1;
Base* pDer2 = new Derived2;
Derived2* pDerCasted = dynamic_cast<Derived2*>(pDer2);
if(pDerCasted)
{
pDerCasted->funcDer2();
}
-> We cannot call funcDer2 with pDer2 as it points to Base class
-> dynamic_cast converts the object to Derived2 footprint
-> in case it fails to do so, it returns NULL .(throws bad_cast in case of reference)
注意:通常情況下,Dynamic_cast應避免仔細的OO設計。
我想你的意思是Derived2 * pDerCasted ..? – lorenzog 2009-11-23 08:39:09
是的,派生的2。我已更新。 – 2009-11-23 08:41:37
- 1. 動態內存究竟是什麼?
- 2. 動態數據究竟是什麼?
- 3. C#中的靜態構造函數究竟是什麼?
- 4. Perl:究竟是什麼C <our>?
- 5. C++中的R值究竟是什麼?
- 6. DEFAULT_COMPRESSION究竟是什麼?
- 7. cacert.pem究竟是什麼?
- 8. JSON-RPC究竟是什麼?
- 9. WPF究竟是什麼?
- 10. NoSQL究竟是什麼?
- 11. 「Sample」究竟是什麼?
- 12. ContextStaticAttribute究竟是什麼?
- 13. JSON究竟是什麼?
- 14. 究竟是什麼插座
- 15. targetSdkVersion究竟是什麼?
- 16. getGlobalVisibleRect()究竟是什麼?
- 17. 究竟是什麼#^:_ 1
- 18. WCF - 究竟是什麼?
- 19. Rest API究竟是什麼
- 20. 「句柄」究竟是什麼?
- 21. this.id究竟是什麼?
- 22. Erlang OTP究竟是什麼?
- 23. selenese html究竟是什麼?
- 24. 資源究竟是什麼?
- 25. 究竟是什麼@ ViewDebug.ExportedProperty?
- 26. ViewUserControl.ViewDataKey究竟是什麼?
- 27. 究竟是什麼任務
- 28. Windows AppFabric究竟是什麼?
- 29. 「OAuth Provider」究竟是什麼?
- 30. Django Apps究竟是什麼
dup http://stackoverflow.com/questions/28002/regular-cast-vs-staticcast-vs-dynamiccast – Macke 2009-11-23 11:22:56