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:

  1. simply assign in child-class constructor

    woodenboxclass() {     indicator = 'b'; } 
  2. 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') {}     ... }; 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -