By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

Reference to a Template

P: n/a
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;
}


Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Agent Mulder" <mb*******************@home.nl> wrote...
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<class A> struct Wings : BaseForWings {};
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
Jul 22 '05 #2

P: n/a
"Agent Mulder" <mb*******************@home.nl> wrote in message
news:bp**********@news1.tilbu1.nb.home.nl...
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
{...}

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.