#include "stdafx.h"
#include <assert.h>
class IInterface1
{
public:
virtual void VFunc1() = 0;
};
class IInterface2
{
public:
virtual void VFunc2() = 0;
};
class TestClass1:
public IInterface1, public IInterface2
{
void VFunc1();
void VFunc2();
};
void TestClass1::VFunc1()
{
;
}
void TestClass1::VFunc2()
{
;
}
class Interface12:
public IInterface1, public IInterface2
{
};
template<class Intttt>
Intttt* MakeInterface2(IInterface1* pInterface1)
{
return static_cast<Intttt*>( static_cast<Interface12>(pInterface1) );
}
int _tmain(int argc, _TCHAR* argv[])
{
TestClass1* pTestClass1 = new TestClass1();
IInterface1* pInterface1 = dynamic_cast<IInterface1*>(pTestClass1);
assert(pInterface1);
IInterface2* pInterface2 = static_cast<IInterface2*>(pInterface1);
IInterface2* pInterface2 = MakeInterface2<IInterface2>(pInterface1);
return 0;
}
The red line code will cause compile error:
error C2440: 'static_cast' : cannot convert from 'IInterface1 *' to 'IInterface2 *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast