c++ - Using variables from within parent class -


i have structure of classes:

class {   class b;   class c;   int updates = 0;  // invalid use of non-static data member 'a::updates'   c* example;   class b {    public:     // ...     void up_plus() {       updates++;  // problem here     }     // , other methods.......   };   class c : public b {    public:     int size;     // ...     void sizeup() {       example->size++;  // invalid use of non-static data member 'a::header'     }     // , other methods....   }; }; 

my question is, how can fix structure? in java work, here there problem.

the syntax;

class {     int updates = 0; // allowed in c++11 , above // ... 

is allowed compiling c++11 standard , above. assuming using clang or g++, add -std=c++11 or -std=c++14 command line.

secondly, nested classes not have immediate access instance of outer class. still need constructed pointer or reference "parent". given inheritance relationship of b , c, following suitable you;

  class b {    protected:     a* parent_;    public:     b(a* parent) : parent_(parent) {}     void up_plus() {       parent_->updates++; // addition of parent_     }   };   class c : public b {    public:     int size;     void sizeup() {       parent_->example->size++; // addition of parent_     }   }; 

working sample here.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -