c++ - How to call super constructor from constructor with default params? -


i have 2 c++ classes, base class , sub class. base class defines properties , sub class others. now, in sub class constructor defined several default parameters , needs include default params base class, too. how do that?

example:

// base.h class base {     protected:         state _state;         state _previousstate;      public: base(); };  // sub.h class sub : public base {     private:         int _rate;         int _checkcount;      public: explicit sub(int samplerate); };  // sub.cpp sub::sub(int rate)     :_state(silent),     _previousstate(running),     _rate(rate),     _checkcount(0) { } 

obviously, in sub.ccp constructor _state , _previousstate args misplaced. how can integrate them construction?

you can offer default construct function default values base class


// base.h class base {     protected:         state _state;         state _previousstate;      public: base( state =silent,state b =runing):_state(a), _previousstate(b){} };  // sub.h class sub : public base {     private:         int _rate;         int _checkcount;      public: explicit sub(int samplerate); };  // sub.cpp sub::sub(int rate)     _rate(rate),     _checkcount(0) { } 

another way: since data member in base class protected,you can change value in constructor function body of sub class

// base.h     class base {         protected:             state _state;             state _previousstate;          public: base( );     };  // sub.h class sub : public base {     private:         int _rate;         int _checkcount;      public: explicit sub(int samplerate); };  // sub.cpp     sub::sub(int rate)         _rate(rate),         _checkcount(0)     {      _state=slience;      _previousstate=running;     } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -