Advertisement
Dijuna

Inner/Outer

Sep 17th, 2016
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <QApplication>
  2. #include <QDebug>
  3.  
  4. class InnerClass;
  5.  
  6. struct OuterClass
  7. {
  8.     const QList<InnerClass*>& list() const
  9.     {
  10.         return _list;
  11.     }
  12.     QList<InnerClass*> _list;
  13. };
  14.  
  15. struct InnerClass
  16. {
  17.     OuterClass const*const parent;
  18.  
  19.     InnerClass(OuterClass *parent)
  20.         : parent { parent }
  21.     {
  22.  
  23.     }
  24.  
  25.     int index() const
  26.     {
  27.         return parent->list().indexOf(this);
  28.     }
  29. };
  30.  
  31. void test() {
  32.     OuterClass outer;
  33.     outer._list.append(new InnerClass(&outer));
  34.     outer._list.append(new InnerClass(&outer));
  35.     outer._list.append(new InnerClass(&outer));
  36.     qDebug() << outer.list().last()->index();
  37. }
  38.  
  39. int main(int argc, char *argv[])
  40. {
  41.     QApplication a(argc, argv);
  42.  
  43.     test();
  44.  
  45.     return a.exec();
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement