Jump to content
  • Advertisement
Sign in to follow this  
Laval B

Virtual Methods And Typecasting

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

Hello everyone.

If i have a base class CBase with some virtual methods and a derived class CDerived that implements (or overrides) those methods. If the virtual methods are called from a pointer of type CDerived, is the compiler able to bypass the vtable lookup and make a "static" function call ?

Thank you.

Share this post


Link to post
Share on other sites
Advertisement
Not automatically, since a pointer of type CDerived may refer to a further-derived instance. There are a fair number of cases where the compiler can guarantee the dynamic type, however, and in those instances there will be no vtable lookup.

Share this post


Link to post
Share on other sites
Thank you for your reply. That totally makes sense and this is the case, there is another derived class. So if i really want to make sure the call to the methods are statically resolved, i have to make those methods non-virtual in CDerived then call them form a pointer of type CDerived.

Share this post


Link to post
Share on other sites

So if i really want to make sure the call to the methods are statically resolved, i have to make those methods non-virtual in CDerived then call them form a pointer of type CDerived.


Or qualify the method call with the name of one of the bases e.g. CBase::theMethod() or CDerived::theMethod().

Share this post


Link to post
Share on other sites

i have to make those methods non-virtual in CDerived


Please note that you can't do this if the method is virtual in the base class. Once virtual, always virtual.

Share this post


Link to post
Share on other sites

Please note that you can't do this if the method is virtual in the base class. Once virtual, always virtual.


Yes of course.

As for the qualification using [color=#1C2837][size=2]CDerived::theMethod(), isn't it only going to work on static methods ?
[color=#1C2837][size=2]

Share this post


Link to post
Share on other sites

No, it works perfectly fine for non-static member functions.


How does the compiler know which instance to work on ? Besides, if i write a call like CDerived::theMethod(), the compiler tells me that this is an invalid call of a non-static function (error C2352).

Share this post


Link to post
Share on other sites
The compiler knows what instance to call it on the same way it knows what instance to call it on with an unqualified function name: because you give it an instance. Ex:

#include <iostream>

struct A {
virtual void foo(void) { std::cout << "A::foo" << std::endl; }
virtual ~A() {}
};

struct B : A {
virtual void foo(void) { std::cout << "B::foo" << std::endl; }
virtual ~B() {}
};

int main(int, char **) {
B b;
b.A::foo();
}

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!