Reference to a Template

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;
}
 
V

Victor Bazarov

Agent Mulder said:
I try to get a reference to a template.

It's impossible. A template is something that exists only
during compile-time. A reference is something that designates
an object. A template is not an object.
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.

No. A template is not a base. Two different instantiations
of the template are TWO DIFFERENT bases.
Because I don't know what classes I am dealing with, I try to get
a reference to the base class.

There is _no_ base class. There are two _distinct_ base classes.
Since the base class is a template
instaniation, I cannot find a common type for all classes that
inherit the template.

Then make your template inherit from a _real_ class, which then
will become the base for all template instantiations.
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;};

You might do this:

struct BaseForWings { virtual void fly() = 0; };
template said:
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 ???

There is no way. You could only get a reference to BaseForWings
struct in all of them.
return 0;
}

Victor
 
C

Cy Edmunds

Agent Mulder said:
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;
}


If you are designing a set of classes which use templates and polymorphism
both it is generally a good idea to do the polymorphic part first and add
the template part later:

class IFlier // polymorphic interface
{
virtual void fly() = 0;
virtual ~IFlier() {}
};

template <typename T>
class FlyingPig : public IFlier // templated implementation
{...}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top