0

This will probably come off as a really dumb question. I'm really confused as to whether I should be instantiating a object from the sf::Sprite class or inheriting it? Here is what I mean:

Mario.h

class Mario : public sf::Sprite {}

Mario.h

class Mario{
  sf::Sprite m_sprite;
}

At the end of the day, shouldn't it be possible to draw the sprite on screen either way? If so, what's the ideal/common practice and is there any possible advantages of inheriting over instantiating or vice versa in this case?

rohanharikr
  • 1,201
  • 1
  • 13
  • 28
  • If you have a Car class, do you inherit engine or instantiate engine inside car? – Sreeraj Chundayil Aug 06 '21 at 12:01
  • Pretty sure in SFML you can inherit from `Drawable` instead. I'd at least prefer that over inheriting `sf::Sprite`. – mediocrevegetable1 Aug 06 '21 at 12:02
  • Is Mario a sprite, or does Mario have a sprite? – JaMiT Aug 06 '21 at 12:03
  • @JaMiT that's my doubt too, should I be creating a sf::Sprite (SFML class) in a non-derived Mario class or should I be inheriting all the methods of sf::Sprite in my Mario class? – rohanharikr Aug 06 '21 at 12:07
  • @InQusitive sorry, not sure I understand – rohanharikr Aug 06 '21 at 12:10
  • @mediocrevegetable1 doesnt Sprite inherit from Drawable? I would prefer to use Sprite directly because it has some additional methods like move, etc – rohanharikr Aug 06 '21 at 12:11
  • Ignore, I read the question incorrect I think. – Sreeraj Chundayil Aug 06 '21 at 12:12
  • @rohanharikr my personal opinion would be to inherit from `sf::Drawable` for `draw` and instantiate the object inside `Mario` because it sounds like `Mario` is a player instead of just a sprite. – mediocrevegetable1 Aug 06 '21 at 12:13
  • @mediocrevegetable1 Yes, Mario is a player class. If I am instantiating sf::Sprite inside Mario, what's the need to inherit from sf::Drawable again because sf::Sprite already inherits from sf::Drawable and I can draw the sprite directly to window by passing the window object reference to Mario class? – rohanharikr Aug 06 '21 at 12:16
  • 4
    Does this answer your question? [Prefer composition over inheritance?](https://stackoverflow.com/questions/49002/prefer-composition-over-inheritance) – Mestkon Aug 06 '21 at 12:24
  • 1
    Found a duplicate in the SFML forum: https://en.sfml-dev.org/forums/index.php?topic=9772.0 – mediocrevegetable1 Aug 06 '21 at 12:27
  • @Mestkon thanks for the link, but I'm still confused when applying the same to this case because Mario is a sprite and Mario also has a sprite? – rohanharikr Aug 06 '21 at 12:30
  • @mediocrevegetable1 thanks for the link, can't believe I didn't find it! – rohanharikr Aug 06 '21 at 12:30
  • 1
    From Mestkon's link, [this answer](https://stackoverflow.com/a/49016/13188071) seems especially relevant: the sprite is only one feature of `Mario` and there are likely to be other things like physics/stats. – mediocrevegetable1 Aug 06 '21 at 12:32
  • @mediocrevegetable1 starting to make a bit more sense now but it's still possible to add methods after inheriting sf::Sprite right? – rohanharikr Aug 06 '21 at 12:34
  • 2
    @rohanharikr I would argue that Mario is actually a playable character which incidentally has a sprite amongst other features – Mestkon Aug 06 '21 at 12:37
  • 1
    It should be, yes. But ultimately the point of that answer (and inheritance in general) is that when you inherit something you essentially mean "Derived is a type of/variant of Base," and a player is more than that. – mediocrevegetable1 Aug 06 '21 at 12:38
  • 1
    If you can argue that `Mario` *is* a particular type of `Sprite`, then inheritance is an option to consider. If you can argue that each `Mario` has a number of properties, one of which is a sprite (e.g. a self-contained object that is used by a `Mario` and other objects to paint some evolving representation of themselves on a display), then composition is an appropriate choice. If you can make both arguments, then prefer composition over inheritance – Peter Aug 06 '21 at 12:56
  • @Peter thanks, that makes sense – rohanharikr Aug 06 '21 at 13:00
  • Note that what you call "instantiation" in this question is more commonly called "composition". In both your examples, a `sf::Sprite` would be _instantiated_ – Drew Dormann Aug 06 '21 at 14:33

0 Answers0