Re: Can we use abstract class templating classes?
newbie wrote:
Say I have the following class:
class MyAbs {
virtual ~MyAbs() {}
virtual void foo() = 0;
virtual void bar() {cout << "abs::bar"; }
}
class MyDer1: public MyAbs {
MyDer1() {counter = 0; }
foo() { cout << "der1::foo--" << counter++; }
int counter;
}
class MyDer2: public MyAbs {
MyDer1() {counter = 100; }
foo() { cout << "der1::foo--" << counter--; }
int counter;
}
-----------------------------------------
Can I do something like this? Thanks
template <class Toy>
class MyTemplateClass{
Toy toy_;
Play() { toy_.foo(); }
}
Yeah, but you don't need to. You are using generics (although a limited
form if I understand the term correctly) in conjunction with templates
when it isn't really necessary *in the scope of this case*. You could
do the same thing without subclassing MyAbs. Of course whatever
MyTemplateClass takes as a parameter, it must have a function foo that
takes no parameters.
Generics is a runtime solution, "I want to plug in any class at runtime
that has this interface so that I may interact with it using those
interfaces", as opposed to templates which is a compile time solution "I
want my programme to emit code that will interact with this set of
classes/or constants in the way I define."
Although, by specifying the interface class, it does make it easy to see
what functions are needed. But again it is not necessary *in the scope
of this case*.
Adrian
--
_____________________________________________________________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ My newsgroup writings are licensed under the Creative Commons /
\ Attribution-Noncommercial-Share Alike 3.0 License /
\_____[http://creativecommons.org/licenses/by-nc-sa/3.0/]_____/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/