Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Virtual Template or Template Virtual

Reply
Thread Tools

Virtual Template or Template Virtual

 
 
pocmatos@gmail.com
Guest
Posts: n/a
 
      01-23-2006
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-23-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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
 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      01-23-2006
Victor Bazarov wrote:
> (E-Mail Removed) wrote:
> > 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


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

http://groups.google.com/group/comp....cfcf8fcfd7b913

Cheers! --M

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      01-23-2006
mlimber wrote:
> Victor Bazarov wrote:
>
>>(E-Mail Removed) wrote:
>>
>>>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

>
>
> Victor is correct, as usual. However, see this old post for an
> alternative approach that may suit you:
>
> http://groups.google.com/group/comp....cfcf8fcfd7b913


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
 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      01-23-2006
Victor Bazarov wrote:
> mlimber wrote:
> > Victor Bazarov wrote:
> >
> >>(E-Mail Removed) wrote:
> >>
> >>>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

> >
> >
> > Victor is correct, as usual. However, see this old post for an
> > alternative approach that may suit you:
> >
> > http://groups.google.com/group/comp....cfcf8fcfd7b913

>
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
template template arguments: expected a class template, got `Component<T1, T2, T3> gary.bernstein@gmail.com C++ 1 06-08-2007 07:10 AM
Non-template class from a template base class with pure virtual methods vilarneto@gmail.com C++ 2 03-25-2007 08:19 PM
virtual template and virtual access for ADSL circuits Gary Cisco 1 04-28-2005 07:26 PM
Re: A Newbie Question about template template template tom_usenet C++ 0 07-24-2003 12:06 PM
Re: A Newbie Question about template template template Chris Theis C++ 2 07-24-2003 09:42 AM



Advertisments