Virtual Template or Template Virtual

P

pocmatos

Hi all,

I have an abstract class acting as interface to a given class of
objects. And mostly everywhere around my program I'm passing things
like: vector<int>, list<unsigned long>, list<myclass*>, just to access
the elements it contains. Today I decided to refactor and to try
templates.
So I have abstract class A with function foo which receives
list<myclass*>. And now I want it to receive an Input Iterator
(following the idea behind <algorithm>). So I try this:
template<class InputIterator>
virtual void foo(InputIterator first, InputIterator last) = 0;

Then I have B inheriting from A, defining the function foo. However it
seems the compiler doesn't like template<...> virtual... I also tried
the other way around (virtual template...) but it is of no use.

Any ideas on how to do this is _extremely_ welcome.

Oh, by the way... "Effective C++" book is on its way... Heard great
things about it. Hope its true!

Cheers,

Paulo Matos
 
V

Victor Bazarov

I have an abstract class acting as interface to a given class of
objects. And mostly everywhere around my program I'm passing things
like: vector<int>, list<unsigned long>, list<myclass*>, just to access
the elements it contains. Today I decided to refactor and to try
templates.
So I have abstract class A with function foo which receives
list<myclass*>. And now I want it to receive an Input Iterator
(following the idea behind <algorithm>). So I try this:
template<class InputIterator>
virtual void foo(InputIterator first, InputIterator last) = 0;

Can't do that. A virtual function cannot be a template. Or vice versa.
Then I have B inheriting from A, defining the function foo. However it
seems the compiler doesn't like template<...> virtual... I also tried
the other way around (virtual template...) but it is of no use.

Yes. Prohibited.
Any ideas on how to do this is _extremely_ welcome.

Don't make it a template or don't make it virtual.
Oh, by the way... "Effective C++" book is on its way... Heard great
things about it. Hope its true!

It's not bad.

V
 
V

Victor Bazarov

mlimber said:
Victor is correct, as usual. However, see this old post for an
alternative approach that may suit you:

http://groups.google.com/group/comp.lang.c++/msg/c2cfcf8fcfd7b913

I am not sure how this solves the OP's problem. Notice, I didn't say it
didn't solve it. I just said I wasn't sure. Mostly because the OP didn't
actually say what problem (aside from unifying passing different types of
containers around) he's trying to solve.

It is possible that the whole class needs to become a template.

It is also possible that those virtual functions simply need to be
overloaded based on the type (and thus the need in templates will vanish).
Perhaps the OP can still achieve what he needs by supplying a limited set
of functions...

virtual void foo(list<int>::iterator ...
virtual void foo(vector<unsigned long>::iterator ...
virtual void foo(list<myclass*>::iterator ...

V
 
M

mlimber

Victor said:
I am not sure how this solves the OP's problem. Notice, I didn't say it
didn't solve it. I just said I wasn't sure. Mostly because the OP didn't
actually say what problem (aside from unifying passing different types of
containers around) he's trying to solve.

It is possible that the whole class needs to become a template.

It is also possible that those virtual functions simply need to be
overloaded based on the type (and thus the need in templates will vanish).
Perhaps the OP can still achieve what he needs by supplying a limited set
of functions...

virtual void foo(list<int>::iterator ...
virtual void foo(vector<unsigned long>::iterator ...
virtual void foo(list<myclass*>::iterator ...

V

Again, Victor is correct. There are a number of potential options, but
we can't choose between them based on the information given.

Cheers! --M
 

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
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top