2

Alright lets say I have a cPlayer class that inherits from cOrganism, which inherits from cEntity. Now I'm ready to expand my cPlayer class and i want to add animation to my player. Would it be better to

A.) Create a cAnimation class and have cPlayer inherit from it?

B.) Create a cAnimation class and have it passed to cPlayer as an argument?

C.) Something else?

Nick Savage
  • 856
  • 1
  • 6
  • 18
  • Don't use hungarian notation. It's a bad idea. – bames53 Nov 14 '11 at 16:17
  • @bames53: blurting criticisms of trivial naming conventions is neither constructive, nor on-topic for this question. – tenfour Nov 14 '11 at 16:26
  • @bames53 I assure you that the only form of Hungarian notation used in any of my programs is for my classes. My naming methods are my own and I'd appreciate if you didn't bash them without any knowledge pertaining to them or any reason for them being inadequate. – Nick Savage Nov 14 '11 at 16:50
  • 1
    @tenfour That's why I said it in a comment and not in an answer. It's important for people to know that hungarian notation is a bad idea. – bames53 Nov 14 '11 at 16:51
  • @NickSavage I'm glad you don't use it anywhere else. Your naming methods are indeed your own, but I'm not bashing your naming scheme specifically, I'm only bashing hungarian notation. I made no assumptions about your scheme other than what's evident from your question. Anyway, I think my comment has been blown out of proportion. – bames53 Nov 14 '11 at 16:58
  • @bames53 No harm, no foul. You simply had a statement without anything to back it up. That's why, at least in my case, I responded that way. – Nick Savage Nov 14 '11 at 17:40

3 Answers3

4

It depends on how CAnimation is designed. In general, inheritance forms either a "Implements" or "Is a" relationship between the parent and child classes.

For example, cPlayer should inherit cAnimation went you want either:

  • cPlayer to implement the interface cAnimation
  • or, cPlayer to be a cAnimation.

Neither seem to make any sense to me. "cAnimation" is not the kind of name you'd give an interface, and a player is not an animation.

But this is just based on your naming. Your actual design is going to depend on the relationships between what's controlling the animation, how it interacts with objects (maybe you need a IAnimatedObject interface?), etc.

tenfour
  • 33,679
  • 12
  • 73
  • 135
  • Can I ask you to describe your usage of 'interface'? – Nick Savage Nov 14 '11 at 16:51
  • It's mostly conceptual. The idea is that interfaces just describe how you can interact with an object -- usually interfaces in C++ contain only pure virtual methods with no implementation in the interface itself. `IAnimatedObject` might have `{ virtual void Render() = 0; virtual void UpdateFrame() = 0; }`. Practically speaking, that's about the only difference. Check out [this question](http://stackoverflow.com/questions/318064/how-do-you-declare-an-interface-in-c) for more on interfaces in C++. – tenfour Nov 14 '11 at 17:00
2

I would use composition: make object of type cAnimation as member of cPlayer. Many questions here are related to this problem (inheritance vs polymorphism). One with nice answers is here

Community
  • 1
  • 1
Bojan Komazec
  • 8,186
  • 2
  • 33
  • 48
0

I would rather think that a hypothetical cAnimation class would contain, for example, a sequence of bone positions and that there may be multiple possible animations of a cPlayer (walk, run, jump, etc), thus a set of cAnimations would be a member of cPlayer.

chill
  • 15,637
  • 2
  • 35
  • 43