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

Can we use abstract class templating classes?

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

Mar 16 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"newbie" <mi******@yahoo.comwrote in message
news:11**********************@n59g2000hsh.googlegr oups.com...
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(); }
}
Your code shows classes MyAbs, MyDer1 and MyDer2, yet your template shows
Toy. I'm presuming in my response that by
Toy toy_;
Play() ( toy_.foo(); )

you actually meant
MyAbs toy_;
Play() { toy_.foo(); }

No. Your template is attempting to instantize a class MyAbs which is a
virtual class. You couldn't do it in main so you couldn't do it in a
template. However, I believe you could do:

MyAbs* toy_ = new MyDer1;
Play() { toy_->foo(); }
Mar 16 '07 #2

P: n/a
On Mar 16, 4:37 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"newbie" <mitbb...@yahoo.comwrote in message

news:11**********************@n59g2000hsh.googlegr oups.com...
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(); }
}

Your code shows classes MyAbs, MyDer1 and MyDer2, yet your template shows
Toy. I'm presuming in my response that by
Toy toy_;
Play() ( toy_.foo(); )

you actually meant
MyAbs toy_;
Play() { toy_.foo(); }

No. Your template is attempting to instantize a class MyAbs which is a
virtual class. You couldn't do it in main so you couldn't do it in a
template. However, I believe you could do:

MyAbs* toy_ = new MyDer1;
Play() { toy_->foo(); }
Thanks. I am not sure

What I really want is to ask class MyTemplateClass behave clever
enough to
understand things like

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

template <class Toy>
class MyTemplateClass{
Toy toy_;
Play() { toy_.foo();
}

typedef MyTemplateClass<MyDer1MyClass1;
typedef MyTemplateClass<MyDer2MyClass2;
////////////////////////////////////////
int main() {
MyClass1 m1;
MyClass2 m2;
m1.Play();
m2.Play();
return 0;
}

Mar 16 '07 #3

P: n/a
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/...sa/3.0/]_____/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/
Mar 17 '07 #4

P: n/a
newbie wrote:
On Mar 16, 4:37 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
>"newbie" <mitbb...@yahoo.comwrote in message

news:11**********************@n59g2000hsh.googleg roups.com...
>>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(); }
}
Your code shows classes MyAbs, MyDer1 and MyDer2, yet your template shows
Toy. I'm presuming in my response that by
Toy toy_;
Play() ( toy_.foo(); )

you actually meant
MyAbs toy_;
Play() { toy_.foo(); }

No. Your template is attempting to instantize a class MyAbs which is a
virtual class. You couldn't do it in main so you couldn't do it in a
template. However, I believe you could do:

MyAbs* toy_ = new MyDer1;
Play() { toy_->foo(); }

Thanks. I am not sure

What I really want is to ask class MyTemplateClass behave clever
enough to
understand things like

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

template <class Toy>
class MyTemplateClass{
Toy toy_;
Play() { toy_.foo();
}

typedef MyTemplateClass<MyDer1MyClass1;
typedef MyTemplateClass<MyDer2MyClass2;
////////////////////////////////////////
int main() {
MyClass1 m1;
MyClass2 m2;
m1.Play();
m2.Play();
return 0;
}
Again, yes, but unless you are going to pass out the object to something
that is going to take a MyAbs object which you may pass MyDer1 or
MyDer2, you do not need to subclass MyAbs to make it work.
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/...sa/3.0/]_____/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/
Mar 17 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.