|
#1
|
|||
|
|||
|
virtual overhead
I started out with a class that need some functions to be virtual for the usual reasons. As the code was scrubbed, that reason went away. It led me to a question: If a class (that does not inherit from anyone, so it is a base class) uses the virtual keyword for a function that never gets overridden in a derived class (since there are no derived classes), should I expect the "virtual" keyword to force a vtable lookup when the function is called?
Put another way, should I expect the compiler to recognize that a vtable for is not necessary and do the binding directly? John |
|
#2
|
||||
|
||||
|
Sadly, the answer is yes, but only if you call a member function through a pointer or a reference. A call to a member function via an object is always resolved statically.
Clever compilers and compilers that do whole program analysis are able to optimize away vtable lookup when the function called through a pointer/reference can be resolved statically but don't count on that. So the real question is: does your code call the member functions through pointers or references? Notice that in any event, virtual implies a vtable per class so the space overhead is inevitable.
__________________
Danny Kalev |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Pure Virtual functions | ami | C++ | 2 | 12-02-2006 04:29 PM |
| Pointer to virtual memfunction behaving surpriseingly. | sudipsand | C++ | 4 | 10-28-2005 12:00 AM |
| non virtual destructor is called | drkybelk | C++ | 3 | 03-17-2005 09:27 AM |
| files | langevin | .NET | 1 | 09-13-2002 09:57 PM |
| WebFroms and Virtual Directories | Madhu | ASP.NET | 2 | 05-08-2002 12:36 PM |