473,326 Members | 2,081 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,326 software developers and data experts.

templates vs. virtual methods

I'm thinking about using templates instead of virtual methods to boost
the performance of my library. I would like to know if it is possible
to do something like the following:

template<class T>
void just_do_it(T* t)
{
t->do_it();
}

Of course we do not know whether class T has do_it() method or not. But
will this work out? For the following example it would be cool if it
would generate compile error if MyClass doesn't have do_it() method,
and compile correctly if it has:

MyClass myc = new MyClass();
just_do_it<MyClass>(my);

So, is it possible? If not, then why? I think it would be very nice :)

Jul 23 '05 #1
6 1587
an************@yahoo.com wrote:
I'm thinking about using templates instead of virtual methods to boost
the performance of my library. I would like to know if it is possible
to do something like the following:

template<class T>
void just_do_it(T* t)
{
t->do_it();
}

Of course we do not know whether class T has do_it() method or not. But
will this work out?
If 'T' has no 'do_it', then the compilation will fail.
For the following example it would be cool if it
would generate compile error if MyClass doesn't have do_it() method,
and compile correctly if it has:

MyClass myc = new MyClass();
just_do_it<MyClass>(my);

So, is it possible? If not, then why? I think it would be very nice :)


Yes, it is possible. Just try it and you'll see.

V
Jul 23 '05 #2
Great! I think that suits my needs... Thanks.

Jul 23 '05 #3
an************@yahoo.com wrote:
I'm thinking about using templates instead of virtual methods to boost
the performance of my library. I would like to know if it is possible
to do something like the following:
Templates and virtual methods do very different things. I have rarely
seen a situation in which both were able to accomplish the same goal.
template<class T>
void just_do_it(T* t)
{
t->do_it();
}


Why wouldn't you just call t->do_it()? If the types are known at
compile-time, then t->do_it() would "just work", and if they aren't,
then t->do_it() will still required a virtual method lookup.

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
Jul 23 '05 #4
I am implementing an algorithm, which is general for various types of
entities (neurons in neural network). And I would like to easily reuse
this algorithm for the new types of neurons in the future. I would like
my library be similar to STL, but with its own algorithtms. So,
t->do_it() will work of course, but my purpose is to create a library
of algorithtms. I already have a working version with virtual methods
(in Delphi), but it's now clear for me that the additional level of
abstraction I have is unnecessary because I'm not really in a situation
when the entities (neurons) may be replaced dynamically. Thank you! I
would also like to slightly change my initial question:

Is it _normal_ to do something like the following? Isn't it a bad
_style_ of using C++?

template<class T>
void just_do_it(T* t)
{
t->do_it();
}

For example, STL algorithms require container items to have operators
, <, = implemented right. So I suppose I can require my template

classes to have a method I want (like above). Good style or bad?

It seems to me that generic programming requires more thorough studying
than I did before. For example, the code above is somehow similar to
requiring interface implementation without having the interface itself.
This is really interesting alternative... Wow!

Jul 23 '05 #5
classes to have a method I want (like above). Good style or bad?


Requiring classes to have specific methods available: good.

For more information on generic programming I recommend Alexandrescu's
book, Modern C++ Design.

http://www.amazon.com/exec/obidos/AS...eeducation-20/

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
Jul 23 '05 #6
Great! Thank you.

Jul 23 '05 #7

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

Similar topics

3
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hi, I am posting this again, because my previous message dated 18.09. disappeared from the list (someone has cancelled it ? why ??). I have a problem with compiling the...
4
by: Martin | last post by:
Greetings I want to have virtual member functionality, but without my member functions being virtual:-) As of yet this is all just in my head cause I can't see a nice solution yet so lets...
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?
7
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
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...
3
by: Steven T. Hatton | last post by:
Has anybody here used explicit instantiation of templates? Has it worked well? Are there any issues to be aware of? -- NOUN:1. Money or property bequeathed to another by will. 2. Something...
6
by: Hunk | last post by:
Hi I have a question on usage of forward declarations of templates. While searching through this group , people suggested using forward declarations and typedefs for templates as // in...
5
by: Lars Hillebrand | last post by:
Hello, i discovered a weird behaviour if i use templates together with virtual inheritance and method over. I managed to reproduce my problem with a small example: // *********** <code...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.