mardi 29 août 2017

initialize std::array with 'this' pointer

I am trying to initialize an array inside a template class and pass the this pointer to all elements in the array. This is what my classes might look like:

template<int NUM> class outer_class;

template<int N>
class inner_class {
  private:
   outer_class<N> *cl;
  public:
   inner_class(outer_class<N> *num) {
    cl = num;
   }
   void print_num() {
    cl->print_num();
   }

};

template<int NUM> class outer_class {
 private:
  int number = NUM;

  // --> here I basically want NUM times 'this' <-- 
  std::array<inner_class<NUM>, NUM> cl = { this, this, this, this }; 

 public:

  void print_num() {
    std::cout << number << std::endl;
  }

  void print() {
    cl[NUM - 1].print_num();
  }
};

int main() {
  outer_class<4> t;
  t.print();

  return 0;
}

How can I pass the this pointer to all elements of inner_class stored in the array of outer_class (in C++11)?

Aucun commentaire:

Enregistrer un commentaire