Archived

This topic is now archived and is closed to further replies.

virtual function overhead

This topic is 5113 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Dear all, I have some question about virtual function. For example I have a calss and sub-class like this
class base
{
 virtual foo();
}

class child : public base
{
 virtual foo();
}
if I do the following:
// code A

base *l_pBase;

l_pBase = new child();

l_pBase->foo();
Then I will suffer the virtual function overhead, because this call need to look up the virtual table. But what if:
// code B

child *l_pChild;

l_pChild = new child();

l_pChild->foo();
Will I still have the overhead? and what if I don't have the virtual key word in the first place? Code A will not work or something?
class base
{
 foo();
}

class child : public base
{
 foo();
}
[edited by - calus on December 14, 2003 6:12:38 AM]

Share this post


Link to post
Share on other sites
Even if you call foo() on a child pointer, you''ll have the overhead, because, instead of child, you might as well have a class derived from child.

And if you don''t use virtual and call foo() on a pointer to base, you''ll always execute base''s foo() function, no matter whether the object is really a child.

That said, the overhead really isn''t much. If your program is slow, you should probably look for other places first before putting lots of work into an optimization that minor.

Sebastian


---
Just trying to be helpful.

Sebastian Beschke
Just some student from Germany
http://mitglied.lycos.de/xplosiff

Share this post


Link to post
Share on other sites
Thanks for the reply! I am just want to understand them more clearly.

quote:
Even if you call foo() on a child pointer, you''ll have the overhead, because, instead of child, you might as well have a class derived from child.

Hmm... what if that child class is the final leaf level? BTW, am I better take off the "virtual" keyword before the function foo() if I won''t derived any class from child?

Here is another question:
If I make a base class only because I want to put some common function there. I mean I will never declare a variable with the base class, I will always use the derived class, is it better to not use the virtual keyword at all?

And are there any overhead with derived class? I just want to know

Thanks in advace

Share this post


Link to post
Share on other sites
The only places, where I wouldn''t use polymorphism because of performance issues would be for large numbers of small objects like vertices or pixels. Elsewhere the overhead is totally insignificand. A single if()-else would cost you more.

A normal method call consists of 1 call instruction directly to the method.
call MyMethod@MyClass 


A virtual method call consists of 1 call instruction into the vtbl, typically a register subtraction to adjust the this pointer offset and another jmp instruction to call the derived method.
call MyMethod@vtbl@MyBaseClass
sub ecx, 4
jmp MyMethod@MyDerivedClass


Also, the compiler won''t be able to inline the virtual method call.

-Markus-

Share this post


Link to post
Share on other sites