Sign in to follow this  

Class Function Overloading Method Question

This topic is 2046 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.

[CODE]
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);
}
[/CODE]

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
[quote]Is this correct OOP? [/quote]

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

[quote]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.

[CODE]
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);
}
}

[/CODE]

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
[quote]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left][background=rgb(250, 251, 252)]Is this correct OOP?[/background][/left][/size][/font][/color]
[/quote]
Not really. A better way would be to use composition. A PlayerModel [i]has a[/i] 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

This topic is 2046 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this