回楼上,早上有点想当然了,看编译通过了,就没看,也没调试
通过编译是没问题的,你看是不是哪抄错了,但是结果是错的,显示的都是第一个字符串
又认真重新写了一遍~~
#include <iostream>
#include <ObjBase.h>
using namespace std;
typedef int InterFaceIndex;
interface wordfirst :IUnknown
{
public:
virtual void __stdcall speakf()=0;
};
interface wordsecond : IUnknown
{
public:
virtual void __stdcall speaks()=0;
};
static const IID IID_Ifirst =
{0, 0, 0,
{0, 0, 0, 0, 0, 0, 0, 1}} ;
static const IID IID_Isecond =
{0, 0, 0,
{0, 0, 0, 0, 0, 0, 0, 2}} ;
class spkword :public wordfirst,public wordsecond
{
public:
void __stdcall speakf()
{
cout<<"good morning world , I would !"<<endl;
}
void __stdcall speaks()
{
cout<<"早起的屌丝早日抱得女神归"<<endl;
}
virtual HRESULT __stdcall spkword::QueryInterface(const IID& i,void** ppv)
{
if (i==IID_Ifirst)
{
*ppv=static_cast<wordfirst*>(this);
return S_OK;
}
else if (i==IID_Isecond)
{
*ppv=static_cast<wordsecond*>(this);
return S_OK;
}
else
{
cout<<"index error"<<endl;
return E_NOINTERFACE;
}
return S_OK;
}
virtual ULONG __stdcall AddRef() { return 0; }
virtual ULONG __stdcall Release() { return 0; }
};
int __cdecl main()
{
HRESULT hr;
spkword* sp=new spkword;
wordfirst* spf=NULL;
if (SUCCEEDED( hr=sp->QueryInterface(IID_Ifirst,(void**)&spf)))
{
spf->speakf();
}
wordsecond* sps=NULL;
if (SUCCEEDED( hr=sp->QueryInterface(IID_Isecond,(void**)&sps)))
{
sps->speaks();
}
int i;
cin>>i;
delete sp;
return i;
}