Templates containing instances of themselves, recursion in C ++

a very quick question, let's say I have a template:

template <class T>
class foo {

 private:
   T SubFoo;
  ...

};

And then I have things like:

 foo < foo < int > > myFoo;

which works great. In this case, myFoo will have a member myFoo.SubFoo, which will be of type foo < int >.

I want to have a pointer in myFoo.SubFoo that points to myFoo. I do not know how to properly call it, a pointer to the SubFoo class, which points to the entire mother class myFoo. Is it possible? I tried to include an ad:

template <class T>
class foo {

 private:
  ...
   T SubFoo;
   foo< foo < T > >* p2mother; 
  ...

};

But that does not work.

In general, what I am doing is creating a recursive structure. It's very easy to pass a recursive message, but I find the trouble up. Maybe I'm designing it wrong.

Thank you so much!

+3
source share
2

( g ​​++):

template <class T>
struct foo {

 //...
 T SubFoo;
 foo* p2mother; 
 //...
};

foo< int > simple;
foo< foo< int > > complex;

int main() {

  simple.p2mother = &simple;
  complex.p2mother = &complex;
}
+1

( ) VS2010, gcc-4.3.4

template <class T>
struct foo {
   T SubFoo;
   foo< foo < T > >* p2mother; 
};

foo<int> foo_instance;

foo<foo<int> > foo_foo_instance;

int main()
{
  foo_instance.p2mother= &foo_foo_instance;
}
0

All Articles