Jump to content
  • Advertisement
Sign in to follow this  
GraphicsDude

Virtual function table layout w/ inheritance

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

struct Base
{
virtual void Print()
{
cout << "In Base" << endl;
}
};

struct A : public Base
{
virtual void Print()
{
cout << "In A!" << endl;
}
};

struct B : public Base
{
virtual void Print()
{
cout << "In B!" << endl;
}
};

int main()
{
B *pB = new B();
Base *pBase = (Base*)pB;
pA = (A*)pBase;
pA->Print();

system( "pause" );
return 0;
}


This prints "In B!" in VS2008 but is it legal? My coworkers and I are discussing whether it is guaranteed that if you call a virtual function on a pointer, and the virtual function is defined in the base, that it will call the correct version of the function regardless of what the pointer type is (as long as it is derived from the Base) because the function will be in the same slot in the vtable.

Share this post


Link to post
Share on other sites
Advertisement
It isn't legal. Its undefined behaviour. Besides, vtables are an implementation detail, you cannot rely on their presence.

You might get some nasty surprises if you introduced a little multiple inheritance into your example.

Share this post


Link to post
Share on other sites
Yeah I could see that in a general sense. In our real-world example both A and B are in fact same versions of a templated class so all their vtable slots should be identical (same number same order) so while it is technically illegal, I think it is safe. Maybe I'm wrong.

Share this post


Link to post
Share on other sites

Yeah I could see that in a general sense. In our real-world example both A and B are in fact same versions of a templated class so all their vtable slots should be identical (same number same order) so while it is technically illegal, I think it is safe.


The problem isn't in vtable - it's in '(A*)'

C++ has dynamic_, static_ and reinterpret_ cast. Using C-style cast is fine, as long as you take consequences for your choice. It may work as expected in your case, but it's not safe. At very least, use static_cast in place of ().

C++ is interesting because it's a never ending race between trying to master the intricacies of language and C++ trying to come up with increasingly convoluted corner cases. So far, C++ is winning by a wide margin.


Also, come to think of it, I don't think it's legal. It might fall under those aliasing rules which state that same memory may only be occupied by one type and the above does not respect that.

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!