denis wrote:
Hi All,
I'm making a class which derives(implements) from 5 interfaces:
class myClass: public base1, public base2, public base3,.......
I want to be able to ask from any interface about a pointer to another
interface
What I mean is:
myClass _myImpObj;
base1 *b1 = &_myImpObj;
base *b2 = b1->QueryInterafce("b2");
base *b3 = b2->QueryInterafce("b3");
base *b1N = b3->QueryInterface("b1");
Its is very simialir to COM mechanism.Anyone knows how to implement it?
You can use dynamic_cast:
#include <iostream>
template <int N>
struct base{
virtual ~base(){}
};
struct my12 : base<1>, base<2>{};
struct my34 : base<3>, base<4>{};
struct my5 : base<5>{};
struct my12345 : base<1>,base<2>, my34 ,my5{};
template <int N, typename T>
bool has_interface(T * t)
{
bool is = dynamic_cast< base<N* (t) !=0;
std::cout << typeid(T).name() << " "
<< ((is)?"Is":"Is not")<< " a base<" << N << ">\n";
return is;
}
template <int N>
struct interfaces;
template <>
struct interfaces<1>{
template <typename T>
void operator()( T * t)const
{
has_interface<1>(t);
}
};
template <int N>
struct interfaces{
template <typename T>
void operator()( T * t)const
{
interfaces<N-1f;
f(t);
has_interface<N>(t);
}
};
int main()
{
//test 5 interfaces
interfaces<5test;
//test some expressions
my12 var12;
test(&var12);
std::cout <<"---------\n\n";
my34 var34;
test(&var34);
std::cout <<"---------\n\n";
my5 var5;
test(&var5);
std::cout <<"---------\n\n";
my12345 var12345;
test(&var12345);
}
regards
Andy Little