[英]how to query up or query down the interface pointer?
我需要在程序中向上和向下查詢接口指針,
我已經定義了接口
struct Iinterface1
{
methods X1()
}
struct Iinterface2 : Iinterface1
{
methods X2()
}
queryInterface( *interface)
{
returns the pointer to one of above interface
}
我可以使用函數queryInterface()查詢並獲取指向接口的指針
由於interface2稍后出現,因此我需要在程序中同時支持method1和method2,因此我無法擁有指向interface2的接口指針並執行方法x1和方法x2。 我正在尋找的是如何在執行過程中升級或更改接口指針。 所以我將查詢並獲取指向interface1的指針並執行方法x1,然后在我想執行方法x2時,我需要查詢指向interface2的指針,以便執行方法x2。 說得通? 感謝您提供示例幫助。
假設Interface1
有一個虛方法,那么如果您有一個指向Iinterface1
的指針,則可以測試一下它是否是具有動態類型轉換的Iinterface2
:
const Iinterface2 * is_Iinterface2 (const Iinterface1 *p) {
return dynamic_cast<const Iinterface2 *>(p);
}
如果結果為NULL
,則p
並非來自Interface2
派生的對象。
C ++無法動態查詢確定類是否具有特定方法的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.