1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#include <iostream>
using namespace std;
class SomeClass;
/* In the real code I have SomeClass< T, P, Q>
* and this is not known when the compiler reaches A,B and C*/
struct OtherClass{
int i;
double d;
OtherClass(int ii, double dd):i(ii),d(dd){};
};
/* My idea of A is this, although it is not
* possible to have a virtual template function */
class A{
public:
template<class K>
virtual void operator()(K input )=0;
};
template<int L, bool M, class S>
struct B : public A{
template<class K>
void operator()(K input ){ cout << "Inside B" << endl; };
};
template<class S>
struct B<0, true, S> : public A{
template<class K>
void operator()(K input ){ cout << "Inside B<0,true>"<< endl; };
};
/* This class creates and stores an object of type B (or one of the specializations).
* But L and M are unknown so I created A to store the B object here
* K is also unknown here. */
class C{
A* object;
public:
C(){
/*
* Get some data from files and according
* to this data construct a B specialized object. */
object = new B< 0, true, SomeClass>();
};
void go(){
/*
* Later, I want to call B, that was stored through the interface A
*/
(*object)(new OtherClass(0,2));
}
};
int main(void){
C* c = new C();
c->go();
return 0;
};
|