W
WaterWalk
Hello. I am rather confused by the type of a pointer to class data
member. Many c++ texts say that pointer to data member has a special
syntax. For example the following class:
class MyClass
{
public:
int n;
};
The pointer to MyClass.n shall be defined like this:
typedef int MyClass::*pn_t;
pn_t pn = &MyClass::n
MyClass my;
my.*pn = 1;
But at the same time, the following code also works:
MyClass my;
int *pn = &my.n;
*pn = 1;
So I wonder if the second method is legal in the c++ standard. If it
is, why bother to have the first method?
member. Many c++ texts say that pointer to data member has a special
syntax. For example the following class:
class MyClass
{
public:
int n;
};
The pointer to MyClass.n shall be defined like this:
typedef int MyClass::*pn_t;
pn_t pn = &MyClass::n
MyClass my;
my.*pn = 1;
But at the same time, the following code also works:
MyClass my;
int *pn = &my.n;
*pn = 1;
So I wonder if the second method is legal in the c++ standard. If it
is, why bother to have the first method?