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_     }   }; 
Comments
Post a Comment