C++ class inheritance variables -
i'm working on game, , want store character belongs unit inside class defines units. (as objects)
this defining class. (i use inheritance)
class units { public: char indicator; units(); virtual ~units(); protected: private: }; units::units (){} units::~units (){} class woodenboxclass: public units { public: woodenboxclass.indicator = 'b'; };
during compilation, when "woodenboxclass.indicator = 'b';" comes, error message:
50|error: expected unqualified-id before '.' token
what should do? main question how can reach "indicator" variable same every "woodenboxclass" object?
you need initialize member variables in class constructor. there 2 ways it:
simply assign in child-class constructor
woodenboxclass() { indicator = 'b'; }
have constructor in base class takes indicator argument, , use initializer list in child-class constructor:
class unit { ... explicit unit(char ind) : indicator(ind) {} ... }; class woodenboxclass : public unit { ... woodenboxclass() : unit('b') {} ... };