Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I compose pointers to member

I'd like to compose member pointers. Basically I have a main class with different member. How do I create a member pointer for the main class that would point to a member of a member of that class. I hope the code below is explains what I'm trying to do:

  struct SubUnit
  {
    int   value;
  };
  struct Unit
  {
    SubUnit sub_unit;
  };

  void Test()
  {
    SubUnit Unit::* ptr1 = &Unit::sub_unit; // WORKING
    int Unit::* ptr2 = &Unit::sub_unit::value; // NOT WORKING !
  }
like image 309
0x26res Avatar asked Jan 28 '14 15:01

0x26res


1 Answers

It seems you have to do it in two phases:

SubUnit Unit::*pSub = &Unit::sub_unit;
int SubUnit::*pValue = &SubUnit::value;

Unit u;
int theVal = (u.*pSub).*pValue;
like image 68
selalerer Avatar answered Sep 21 '22 11:09

selalerer