Class holding a pointer to data member?

I’m trying this in ROOT 5.34.03 on SLC5, but I should say upfront that this is something which compiles and runs fine in other compilers, eg, DevC++ on Win7.

Anyways, what I would like to do is have an ntuple reader and independently create an object which should be able to return a particular value from said ntuple. The tricky part is sometimes the value is a literal data member, and other times is a function. What I hoped to do is create a small class to hold a pointer to a (data) member and/or a pointer to a member function (perhaps a union), and have one generic method to interact with this. Now, in ROOT I have no problem getting the pointer to member function class to work, but eg, when I try this I am getting an error:

Where testPM.C reads:

class A { // test class
public:
  int x;
  explicit A(int X) : x(X) { }
  int GetX() const { return x; }
};

class PMV {
  typedef int A::*PVar;
  PVar var;
public:
  explicit PMV(PVar v) : var(v) { }
  int GetValue(const A& a) { return a.*var; }
};

void testFunction(int y) {
   A myA(y);
   PMV myPMV(&A::x);
   std::cout << myPMV.GetValue(myA) << std::endl; // I want to be able to do this
}

Here I’d like A to be my ntuple reader. I don’t think I can directly use, eg, TTree::GetBranch() because as I said some of these things are calculated on the fly, and I’d like PMV to actually hold both a pointer to member data and pointer to member function and know which one to use. I can do this in other compilers.

Also, I’ve tried getting this to work with a pointer to member function. This works fine in ROOT, I have no problems. I’ve also tried also the following:

  1. Remove class PMV. Then redefine testFunction as:
void testFunction(int y) {
  A myA(y);
  int A::*pVar = &A::x;
  std::cout << myA.*pVar << std::endl;
}

This works, but doesn’t help me in practice as I don’t always have a data member.

  1. Move the typedef from being private in PMV to being global. I get the same error.

  2. Remove the typedef and change PMV to

class PMV {
  int A::*var;
public:
  explicit PMV(int A::*V) : var(V) { }
// same from here
}

This won’t compile, but has a different error:

testPM_C_ACLiC_dict.cxx:317: error: no matching function for call to ‘PMV::PMV(int)’ testPM.C:36: note: candidates are: PMV::PMV(int A::*) testPM.C:33: note: PMV::PMV(const PMV&) testPM_C_ACLiC_dict.cxx:319: error: no matching function for call to ‘PMV::PMV(int)’ testPM.C:36: note: candidates are: PMV::PMV(int A::*) testPM.C:33: note: PMV::PMV(const PMV&)

Which looks like when I am trying PMV myPMV(&A::x) it seems to think &A::x is an int.