QueryInterface

#include <iostream>
using namespace std;


class Base
{
public:
//virtual void FunBase() = 0;
virtual int QueryInterface(int n,void** ppv) = 0;
};


class SubBaseA:public Base
{
public:
virtual void FunSubBaseA() = 0;
virtual void Cool()
{
cout<<"SubBaseA::Cool"<<endl;
}
};


class SubBaseB:public Base
{
public:
virtual void FunSubBaseB() = 0;
virtual void Cool()
{
cout<<"SubBaseB::Cool"<<endl;
}
};


class SubBaseC:public Base
{
public:
int QueryInterface(int n,void** ppv)
{
return 0;
}
//virtual void FunSubBaseC() = 0;
};


class Derived:public SubBaseA,public SubBaseB,public SubBaseC
{
public:
virtual void Cool()
{
cout<<"Derived::Cool"<<endl;
}
//void FunBase()
//{
// cout<<"Derived::FunBase"<<endl;
//}
void FunSubBaseA()
{
cout<<"Derived::FunSubBaseA"<<endl;
}
void FunSubBaseB()
{
cout<<"Derived::FunSubBaseB"<<endl;
}
//void FunSubBaseC()
//{
// cout<<"Derived::FunSubBaseC"<<endl;
//}


int QueryInterface(int n,void** ppv)
{
if (1 == n)
{
*ppv = (SubBaseA*)this;
}
else if (2 == n)
{
*ppv = (SubBaseB*)this;
}
else
{
*ppv = (SubBaseC*)this;
}


return 0;
}
};
int main()
{
Derived* pObj = new Derived;
pObj->Cool();
Base* pIBase = static_cast<Base*>(static_cast<SubBaseA*>(pObj));


SubBaseA* pIBaseA = NULL;
pIBase->QueryInterface(1,(void**)&pIBaseA);
pIBaseA->FunSubBaseA();


SubBaseC* pIBaseC = NULL;
pIBaseA->QueryInterface(3,(void**)&pIBaseC);


SubBaseB* pIBaseB = NULL;
pIBaseC->QueryInterface(2,(void**)&pIBaseB);
pIBaseB->FunSubBaseB();




//pIBaseC->FunSubBaseC();
return 0;
}
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值