473,378 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Virtual Template or Template Virtual

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

Jan 23 '06 #1
4 1980
po******@gmail.com 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
Jan 23 '06 #2
Victor Bazarov wrote:
po******@gmail.com 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

Jan 23 '06 #3
mlimber wrote:
Victor Bazarov wrote:
po******@gmail.com 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
Jan 23 '06 #4
Victor Bazarov wrote:
mlimber wrote:
Victor Bazarov wrote:
po******@gmail.com 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

Jan 23 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Alex Vinokur | last post by:
========================================= Windows 2000 CYGWIN_NT-5.0 1.3.22(0.78/3/2) GNU gcc version 3.2 20020927 (prerelease) ========================================= Here is some program...
8
by: puzzlecracker | last post by:
Can the template method be virtual and what are the consequences? Can the template inline functions be virtual and what are the consequences?
6
by: RainBow | last post by:
Greetings!! I introduced the so-called "thin-template" pattern for controlling the code bloat caused due to template usage. However, one of the functions in the template happens to be virtual...
3
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. ...
4
by: Stefan Nikolaus | last post by:
Hello, I've run into problems with defining a template, which inherits from a base template and the usage of virtual methods in those. I want to initialize a member variable depending on which...
11
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
5
by: alind | last post by:
I m having a problem with templates. A per C++ std says i cant use override template virtual functions. in other words virtual functions cant be templates due to some vtable size issues. Now i want...
11
by: mathieu | last post by:
Hi there, I don't think I'll be able to describe my issue correctly, so instead I'll just give a pseudo C++ code I am struggling with. Basically I am looking for a 'pure virtual template'...
13
by: Mike -- Email Ignored | last post by:
Is a pure virtual function in allowed in a template base class? In any case, I have one working. Am I skating on thin ice? Thanks, Mike.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.