Jump to content
  • Advertisement
Sign in to follow this  
Leviathan3328

Question abou Virtual functions

This topic is 4926 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

Just a quick question. Do you still get hit with a virtual function penalty if you are not using a pointer to a base class that uses a virtual function but instead using a dervied class that has overriden the function in question but it isnt virtual. For example. class BaseClass { virtual void foo(void) = 0; }; class Dervied:public BaseClass { void foo(void); }; and then somewhere in your code you got the following: ... Dervied derviedInstance = new Dervied; derivedInstance->foo(); ... Will calling foo from a pointer of the derived class take a hit from the virtual function? I hope I have asked this question clearly,

Share this post


Link to post
Share on other sites
Advertisement
Probably no, if the compiler can be sure that Derived itself hasn't subclasses.

Edit: Note that "virtualness" cannot be removed in derived classes; Derived::foo is virtual even though there's no virtual keyword. BTW, in C++, there's also no need to use void to indicate that a function takes no parameters, a simple () suffices.

Share this post


Link to post
Share on other sites
Thanks, I would have figgured that it wouldnt have taken the hit but wasnt 100% sure. And I am ware of the void thing, its just a personal preferance.

Thanks again

Share this post


Link to post
Share on other sites
Yes, you will. A function only needs to be declared virtual in the base class. It's implicitly virtual in all the derived classes, it's just that it's good form to explicitly mark it as virtual in the derived class.

If you do call Derived::foo() from inside a Derived function, it won't use the v-table, but I don't know if you can specify it in the way your example uses (It'd be something like derivedInstance->Derived::foo();)


Actually, I didn't read your question properly. I don't think it will in your case, but only because the compiler knows the type of derivedInstance. If you go about passing a Derived* to a function, the compiler will have to use the v-table, since the actual pointer passed could be derived from Derived.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil SteveActually, I didn't read your question properly. I don't think it will in your case, but only because the compiler knows the type of derivedInstance. If you go about passing a Derived* to a function, the compiler will have to use the v-table, since the actual pointer passed could be derived from Derived.


To illustrate that:

struct B
{
virtual void foo(void){ std::cout << "B::foo\n";}
void bar(void){ std::cout << "B::bar\n"; foo();}
};

struct C : public B
{
void foo(void){ std::cout << "C::foo\n";}
};

void doit(B &b){ b.bar();}

int main(int argc, char* argv[])
{
C c;
doit( c);
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Leviathan3328
Just a quick question. Do you still get hit with a virtual function penalty if you are not using a pointer to a base class that uses a virtual function but instead using a dervied class that has overriden the function in question but it isnt virtual. For example.

class BaseClass
{
virtual void foo(void) = 0;
};

class Dervied:public BaseClass
{
void foo(void);
};

and then somewhere in your code you got the following:

...
Dervied derviedInstance = new Dervied;
derivedInstance->foo();
...

Will calling foo from a pointer of the derived class take a hit from the virtual function? I hope I have asked this question clearly,


Even though you don't use the keyword virtual in Derived, it is still a virtual function because the parent class specifies it as such.

Also, generally, the compiler may not know whether you have further subclassed Derived. So it may not easily know that your pointer or reference to a Derived isn't actually something subclassed from that.

Possibly the compiler may be able to figure out that, but I wouldn't count on it. Since foo() in the parent is virtual, I count all subclasses' foo() as virtual.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!