Jump to content
  • Advertisement
Sign in to follow this  
tomek_zielinski2

c++ virtual functions

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

I've got a problem :
class CIndexBuffer
{
    CIndexBuffer  { go(); }
    virtual void go() { do_something(); }
};


class CDerivedIndexBuffer : public CIndexBuffer
{
    virtual void go() { CIndexBuffer::go(); do_extra_stuff(); }
};

then, when I'm creating CDerivedIndexBuffer go() from CIndexBuffer is called... I don't know how to make this right

Share this post


Link to post
Share on other sites
Advertisement
You don't need to declare go in CDerivedIndexBuffer virtual. And you are calling CIndexBuffer::go() explicitly in CDerivedIndexBuffer::go().

[edit]

Also how are you calling it?

something like this?


CIndexBuffer *pObj = new CDerivedIndexBuffer();
pObj->go();

Share this post


Link to post
Share on other sites
The contructor of an object initialises the vtable for this object.

CDerivedIndexBuffer calls CIndexBuffer, before actually calling its own code. At this time, the ctor of CIndexBuffer initialises the vtable to point to the CIndexBuffer vtable. So calling go() in the ctor of CIndexBuffer will call CIndexBuffer::go().
After that the ctor of CDerivedIndexBuffer sets the vtable to point to the vtable of CDerivedIndexBuffer, which creates the usual behavoir.

Edit:


class CIndexBuffer
{
CIndexBuffer { }
void init() { go(); }
virtual void go() { do_something(); }
};


class CDerivedIndexBuffer : public CIndexBuffer
{
virtual void go() { CIndexBuffer::go(); do_extra_stuff(); }
};


CDerivedIndexBuffer buffer = new CDerivedIndexBuffer();
buffer->init();


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!