473,326 Members | 2,113 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.

avoid polymorphism

I have class:
------------
class Component {
void setEnabled(bool state);
}
------------
I want "setEnabled" don't be virtual (there are many member functions. not virtual for efficient).
If a derived class "Button" does polymorhism in "setEnabled" then in this function:
------------
void func(Component &a) {
a.setEnabled(true);
}
------------
will not be called "Button.setEnabled" but "Component.setEnabled" (because "setEnabled" is not virtual)

So, Because I create a library and I want to be far from these quiet errors
I ask:
Is there a trick to avoid polymorphism in this member function???

Thanks!
Jul 19 '05 #1
12 2090

"<- Chameleon ->" <ch******@hotmail.NOSPAM.com> wrote in message
news:bl**********@nic.grnet.gr...
I have class:
------------
class Component {
void setEnabled(bool state);
}
------------
I want "setEnabled" don't be virtual (there are many member functions. not virtual for efficient). If a derived class "Button" does polymorhism in "setEnabled" then in this function: ------------
void func(Component &a) {
a.setEnabled(true);
}
------------
will not be called "Button.setEnabled" but "Component.setEnabled" (because "setEnabled" is not virtual)
So, Because I create a library and I want to be far from these quiet errors I ask:
Is there a trick to avoid polymorphism in this member function???

Thanks!


I don't understand why you do not want setEnabled to be virtual...? If you
want to be able to call the correct setEnabled via a base class reference,
then it HAS to be virtual! That's the whole purpose of a virtual function.
It has to be one way or the other: either the function overrides a virtual
base class function and you can then call the correct version via a base
class pointer or reference, or else the function hides the base class
function and you have to have an a pointer or reference to the actual
derived class in order to call the derived class' function.

-Howard

Jul 19 '05 #2

"<- Chameleon ->" <ch******@hotmail.NOSPAM.com> wrote in message
news:bl**********@nic.grnet.gr...
I have class:
------------
class Component {
void setEnabled(bool state);
}
------------
I want "setEnabled" don't be virtual (there are many member functions. not virtual for efficient). If a derived class "Button" does polymorhism in "setEnabled" then in this function: ------------
void func(Component &a) {
a.setEnabled(true);
}
------------
will not be called "Button.setEnabled" but "Component.setEnabled" (because "setEnabled" is not virtual)
So, Because I create a library and I want to be far from these quiet errors I ask:
Is there a trick to avoid polymorphism in this member function???


I understand that English is not your first language, but still your
question is very confusing. You are certainly avoiding polymorphism if you
don't use virtual functions. What you want is not clear.
Jul 19 '05 #3
"<- Chameleon ->" <ch******@hotmail.NOSPAM.com> wrote in message
news:bl**********@nic.grnet.gr...
So, Because I create a library and I want to be far from these quiet errors I ask:
Is there a trick to avoid polymorphism in this member function???


Any "trick" you come up with is likely to take as long as doing a virtual
lookup anyway. So, you aren't gaining much by avoiding "virtual" except
making your code harder to understand.
Jul 19 '05 #4
Kevin Saff wrote:

Any "trick" you come up with is likely to take as long as doing a virtual
lookup anyway. So, you aren't gaining much by avoiding "virtual" except
making your code harder to understand.


Just how heavy is a lookup anyway? I know that in Objective-C it is
*very* heavy (takes a good chunk of the running time) but in that
language it shows up in a profile as an actual function call. How is it
done in C++ and what resources is it likely to consume?

NR

Jul 19 '05 #5


Noah Roberts wrote:

Kevin Saff wrote:

Any "trick" you come up with is likely to take as long as doing a virtual
lookup anyway. So, you aren't gaining much by avoiding "virtual" except
making your code harder to understand.


Just how heavy is a lookup anyway? I know that in Objective-C it is
*very* heavy (takes a good chunk of the running time) but in that
language it shows up in a profile as an actual function call. How is it
done in C++ and what resources is it likely to consume?


Usually virtual functions are implemented by having a lookup table associated
with that class (the so called VTable)
If the compiler needs to compile a virtual function call he implements code
to:

use the 'this' pointer of the object to locate the VTable.
offset into the VTable to locate the starting address of the function
Use the address found in the VTable to make the call

So basically it boils down to one memory fetch and an indirect function
call, instead of a direct call.
All in all not much overhead, compared to anything else you could come
up with to implement the same functionality.

If you need polymorphism, you need polymorphism. And the compilers way
to implement it, is probably the best one can get.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #6


Karl Heinz Buchegger wrote:

Noah Roberts wrote:

Kevin Saff wrote:

Any "trick" you come up with is likely to take as long as doing a virtual
lookup anyway. So, you aren't gaining much by avoiding "virtual" except
making your code harder to understand.
Just how heavy is a lookup anyway? I know that in Objective-C it is
*very* heavy (takes a good chunk of the running time) but in that
language it shows up in a profile as an actual function call. How is it
done in C++ and what resources is it likely to consume?


Usually virtual functions are implemented by having a lookup table associated
with that class (the so called VTable)
If the compiler needs to compile a virtual function call he implements code
to:

use the 'this' pointer of the object to locate the VTable.
offset into the VTable to locate the starting address of the function
Use the address found in the VTable to make the call

So basically it boils down to one memory fetch and an indirect function


Sorry: two memory fetches.
First: the object carries a pointer to the vtable, get that
second: use the pointer from 1, add offset and fetch the functions start address
call, instead of a direct call.
All in all not much overhead, compared to anything else you could come
up with to implement the same functionality.

If you need polymorphism, you need polymorphism. And the compilers way
to implement it, is probably the best one can get.

--
Karl Heinz Buchegger
kb******@gascad.at


--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #7
Karl Heinz Buchegger <kb******@gascad.at> wrote:
So basically it boils down to one memory fetch and an indirect function


Sorry: two memory fetches.


Well, the expensive part is the indirect function anyway: if things are
OK, the data will be in the cache and thus the access would be likely to
cost an extra memory cycle. The indirection function on the other hand
is quite costly because it cannot be inlined. Not inlining of functions
in performance critical sections has two separate costs:

- There is some function call setup normally necessary which takes a bunch
of cycles and forces data to be stored in particular registers or memory
locations when parameters or returns are passed around.
- The units for optimization are chopped into smaller pieces, reducing the
effectivity of the optimizer.

Of course, unless profiling indicates that the function is indeed in a
critical section, both aspects are effectively moot: the function should
not be inlined in this case anyway because compilation dependencies are
best avoided unless there are good reasons to accept them.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
Jul 19 '05 #8
> Any "trick" you come up with is likely to take as long as doing a virtual
lookup anyway. So, you aren't gaining much by avoiding "virtual" except
making your code harder to understand.


I have almost 60 member functions in class Component.
If all of these are virtual, efficient will decreased
Jul 19 '05 #9

"<- Chameleon ->" <ch******@hotmail.NOSPAM.com> wrote in message
news:bl**********@nic.grnet.gr...
Any "trick" you come up with is likely to take as long as doing a virtual lookup anyway. So, you aren't gaining much by avoiding "virtual" except
making your code harder to understand.


I have almost 60 member functions in class Component.
If all of these are virtual, efficient will decreased


Actually, if any one of them are virtual, efficiency will be decreased.
Having said that, unless you're coding real-time systems for the space
shuttle T-1 launch sequence, or looping millions of times through nested
function calls for large database transactions, or writing the chess program
to beat Garry Kasparov, it's really not going to make any difference. I
think we need some perspective here.
Jul 19 '05 #10
"<- Chameleon ->" escribió:
I have almost 60 member functions in class Component.
If all of these are virtual, efficient will decreased


If the program does not work as desired, efficiency will be 0.

And probably you don't need yo make all them virtual. It's not an all or
nothing election.

Regards.
Jul 19 '05 #11
Julián Albo <JU********@terra.es> wrote in message news:<3F***************@terra.es>...
"<- Chameleon ->" escribi :
I have almost 60 member functions in class Component.
If all of these are virtual, efficient will decreased


If the program does not work as desired, efficiency will be 0.

And probably you don't need yo make all them virtual. It's not an all or
nothing election.

Regards.


You might be able to use templates. I'm a little rusty on them
myself, but something like

template <typename T>
bool func(T& a)
{
a.setEnabled(false);
}

This will give you a compiler error if setEnabled is not on the type
T.

This wasn't run through a compiler, but something like that should
work.

Jay
Jul 19 '05 #12
jeffc wrote:
Actually, if any one of them are virtual, efficiency will be decreased.
Having said that, unless you're coding real-time systems for the space
shuttle T-1 launch sequence, or looping millions of times through nested
function calls for large database transactions, or writing the chess program
to beat Garry Kasparov, it's really not going to make any difference. I
think we need some perspective here.


More to the point, the virtual mechanism in C++ is almost invariably
going to be faster than anything people will typically code instead to
do the same job. If it's polymorphic behavior you want and need,
virtual functions are almost always the tool for the job, even where
efficiency is concerned.

Just my $0.02

Adam H. Peterson

Jul 19 '05 #13

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

Similar topics

37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
18
by: Ken | last post by:
Hi. Can anyone refer me to any articles about the compatibility between c++ polymorphism and real-time programming? I'm currently on a real-time c++ project, and we're having a discussion...
3
by: E. Robert Tisdale | last post by:
polymorph just means "many form(s)". The definition in plain English http://www.bartleby.com/61/66/P0426600.html and narrower definitions in the context of computer programming ...
4
by: LP | last post by:
Hi, I understand the concept/definition of polymorphism. But what does the term "runtime polymorphism" mean? I was asked to define it during a technical interview. I gave a guy vanilla definition...
13
by: Krivenok Dmitry | last post by:
Hello all! Perhaps the most important feature of dynamic polymorphism is ability to handle heterogeneous collections of objects. ("C++ Templates: The Complete Guide" by David Vandevoorde and...
18
by: Seigfried | last post by:
I have to write a paper about object oriented programming and I'm doing some reading to make sure I understand it. In a book I'm reading, however, polymorphism is defined as: "the ability of two...
2
by: sarathy | last post by:
Hi all, I need a small clarification reg. Templates and Polymorphism. I believe templates is really a good feature, which can be used to implement generic functions and classes. But i doubt...
11
by: chsalvia | last post by:
I've been programming in C++ for a little over 2 years, and I still find myself wondering when I should use polymorphism. Some people claim that polymorphism is such an integral part of C++,...
17
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
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
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.