Jump to content
  • Advertisement
Sign in to follow this  
bullfrog

Class Function Overloading Method Question

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

Hi, I always wondered what is the right thing to do in this situation. This is a quick example, the third inheritance makes no sense but it adds more context to the question.


class CModel
{
void Draw(CRenderer& _rRenderer);
}
class CPlayerModel : public CModel
{
void Draw(CRenderer& _rRenderer, const CVector3& _krPlayerPosition);
}
class CExamplePlayerModel : public CPlayerModel
{
void Draw(CRenderer& _rRenderer, const CVector3& _krPlayerPosition, void* _pAnotherParameter);
}


As the hierarchy extends, the Draw function requires another parameter for the model to draw correctly. Is this correct OOP? What stops a user from using the parents draw function and therefore breaking the game?

Share this post


Link to post
Share on other sites
Advertisement
Is this correct OOP? [/quote]

Depends on what this is supposed to do!
Please explain how this is supposed to work...

What stops a user from using the parents draw function and therefore breaking the game?[/quote]

Nothing in the snippets you've posted here.

Share this post


Link to post
Share on other sites
This is more of a `Is this the correct way of doing it` question. But I have added functionality if it helps.


class CModel
{
void Draw(CRenderer& _rRenderer)
{
rRenderer.DrawIndexBuffer(m_uiIndexBufferId);
}
}


class CPlayerModel : public CModel
{
void Draw(CRenderer& _rRenderer, const CVector3& _krPlayerPosition)
{
SetModelPosition(krPlayerPosition);



CModel::Draw(rRenderer);
}
}



So the user adds a CPlayerModel to their game. They need to call Draw on the CPlayerModel.

They have the choice of either CModel::Draw or CPlayerModel::Draw. Both are available to be called even though CPlayerModel::Draw should be the only function that used be for drawing this object type.

What stops the user from calling the CModel::Draw? Or should the user just have the "Smarts" or do the research before choosing which draw function to use?

Share this post


Link to post
Share on other sites

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]Is this correct OOP?[/background]

[/font]
[/quote]
Not really. A better way would be to use composition. A PlayerModel has a Model. Over-use of inheritance is something that many beginners get into early, though in fairness the main source of this is poor books and examples which focus on inheritance rather than how to build real domain models.

Share this post


Link to post
Share on other sites
Check out the LSP, which is generally regarded a very good practice in OOP:
http://en.wikipedia.org/wiki/Liskov_substitution_principle

Relying on the "smarts" of users is a very error prone approach.

Why make add an extra parameter to Draw, if all that does is modifying the state of the object anyways? It's more intuitive, if "Draw" would always draw the object, and if you want it at another place, call "SetModelPosition".

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!