A
Agent Mulder
Hi group,
I try to get a reference to a template. In the template, a
virtual function is declared. Later in the program, I inherit
from classes created by the template. So the template is the base
of them all.
Because I don't know what classes I am dealing with, I try to get
a reference to the base class. Since the base class is a template
instaniation, I cannot find a common type for all classes that
inherit the template.
In the code below (terse, yes), I try to find a reference to
a Wings<???> object that can contain either a Pig or a Cow. Other
than having the template itself inherit from a base (what I don't
want now, things are getting too complicated), I cannot find out how.
#include<iostream>
#include<string>
struct Pig{};
struct Cow{};
template<class A>struct Wings{virtual void fly()=0;};
struct Pig_On_Wings:Wings<Pig>{void fly(){cout<<"\nFly, Pig, fly...";}};
struct Cow_On_Wings:Wings<Cow>{void fly(){cout<<"\nBye, Cow, bye...";}};
int main(int argc,char**argv)
{
Pig_On_Wings p;
Cow_On_Wings c;
Wings<Pig>&pig=p;
Wings<Cow>&cow=c;
pig.fly();
cow.fly();
//animal.fly(); with animal a reference to a Wings<???> type. How ???
return 0;
}
I try to get a reference to a template. In the template, a
virtual function is declared. Later in the program, I inherit
from classes created by the template. So the template is the base
of them all.
Because I don't know what classes I am dealing with, I try to get
a reference to the base class. Since the base class is a template
instaniation, I cannot find a common type for all classes that
inherit the template.
In the code below (terse, yes), I try to find a reference to
a Wings<???> object that can contain either a Pig or a Cow. Other
than having the template itself inherit from a base (what I don't
want now, things are getting too complicated), I cannot find out how.
#include<iostream>
#include<string>
struct Pig{};
struct Cow{};
template<class A>struct Wings{virtual void fly()=0;};
struct Pig_On_Wings:Wings<Pig>{void fly(){cout<<"\nFly, Pig, fly...";}};
struct Cow_On_Wings:Wings<Cow>{void fly(){cout<<"\nBye, Cow, bye...";}};
int main(int argc,char**argv)
{
Pig_On_Wings p;
Cow_On_Wings c;
Wings<Pig>&pig=p;
Wings<Cow>&cow=c;
pig.fly();
cow.fly();
//animal.fly(); with animal a reference to a Wings<???> type. How ???
return 0;
}