Re: Pointer to class data member question
WaterWalk wrote:
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;
Yeh, pointer to member function is more useful then pointer to member
the existence of *mem_fun* family can somehow defend this.
and we can't take the address of member function from an instance of
some class.
struct A
{
void print(){}
};
int main()
{
void (*pf)();
A a;
&a.print; // illformed
}
so there's no way to do the same thing as you did in your example.
IMHO, pointer to member function / pointer to member has goodness for
late binding, that's to say, such pointers can pointer any member/member
function of the same type after it's declared.
here's an example when we need (or it's good to use) pointer to member:
#include <vector>
#include <algorithm>
#include <iostream>
struct A
{
A (char x, int y, int z)
: x(x), y(y), z(z) {}
char x;
int y;
int z;
};
typedef int A::*pmA;
struct Selector
{
Selector(pmA pm) : pm_(pm) {}
int operator() (A const& a) const
{
return a.*pm_;
}
private:
pmA pm_;
};
template <class T>
struct Printer
{
void operator() (int i) const
{
std::cout << i << ' ';
}
};
int main()
{
std::vector<A> aVec;
std::vector<int> intVec;
for (int i = 0; i < 10; ++i)
aVec.push_back(A((char)i, i, i+1));
std::transform(
aVec.begin(), aVec.end(),
std::back_inserter(intVec),
Selector(&A::y) // 0 1 2 3 4 5 6 7 8 9
// Selector(&A::z) // 1 2 3 4 5 6 7 8 9 10
);
Printer<int> printer;
std::for_each(intVec.begin(), intVec.end(), printer);
}
--
Thanks
Barry