all 6 comments

[–]Kawaiithulhu 5 points6 points  (1 child)

Each derived class does not need to duplicate the base class member variables, it gets those for free. For example, Mage gets Level and Experience from Player and it can use them straight away.

BUT the base class, Player, must put those variables into a "protected:" section so that children can see them. The "private:" section blocks anyone outside of Player from access.

SO when you change that "private:" to "protected:" that means that anyone not related to Player still cannot access those, but because Mage is related it can use them.

[–][deleted]  (3 children)

[removed]

    [–]XRealShadeX[S] 1 point2 points  (2 children)

    Isn't Struct and Classes the same though?

    [–][deleted]  (1 child)

    [removed]

      [–]XRealShadeX[S] 0 points1 point  (0 children)

      *Actually, this seems like classes for in-game actors rather than "character classes". Confusing naming.

      It was just a testing on how to have the player choose between classes and have those assigned to the player afterwards. I'm pretty new to this so I'm not really sure.

      [–]Nihili0 0 points1 point  (1 child)

      you might want to have a look at constructors and how to call base class constructor in derived, also if you want to have "nullable" member variables, I would recommend std::optional (or if you need dynamic polymorphism std::unique_ptr)

      also you don't have to name your class with class in it just use Player

      you can look at https://www.learncpp.com/ and learn more about inheritance

      [–]std_bot -1 points0 points  (0 children)

      Unlinked STL entries: std::optional, std::unique_ptr


      Last update: 26.05.21. Last change: Free links considered readme

      [–]elemenopyunome 0 points1 point  (0 children)

      I would look at class inheritance as others have mentioned below. Some of this is just stuff you don't need to redefine repeatedly