Public Group

This topic is 2258 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
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 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 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 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".

1. 1
2. 2
Rutin
23
3. 3
JoeJ
20
4. 4
5. 5

• 24
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631734
• Total Posts
3001933
×