I am trying to have a pointer to a member variable, however i will be
deriving a number of classes from a base class. Whilst the code below
works, i am wondering if there is a better way of doing this, or indeed
if there are any traps i am missing.
#include <iostream>
class BaseClass
{
public:
int var;
};
class ChildClass : public BaseClass
{
public:
int var2;
};
typedef int (BaseClass::*PInt);
void dosomething(PInt p, BaseClass* pclass)
{
(*pclass.*p)++;
cout << "value: " << *pclass.*p << endl;
}
void main()
{
BaseClass base;
ChildClass child;
base.var = 10;
child.var2 = 20;
PInt p = &BaseClass::var;
PInt p2 = (PInt)&ChildClass::var2;
dosomething(p,&base);
dosomething(p2,&child);
}
// end code!
Thanks in advance,
Chris