has anyone seen this?

A

Amadeus W.M.

I saw somewhere some code that in a simplified version boils down to the
example below. Could someone explain when one would define a pointer as
that in main() below, and why? Pointers to documents that explain this
will do. Thanks!


class MyClass
{
public:
int x;
};


int main()
{
int MyClass::*px = &MyClass::x;

return 0;
}
 
A

Alf P. Steinbach

* Amadeus W.M.:
I saw somewhere some code that in a simplified version boils down to the
example below. Could someone explain when one would define a pointer as
that in main() below, and why? Pointers to documents that explain this
will do. Thanks!


class MyClass
{
public:
int x;
};


int main()
{
int MyClass::*px = &MyClass::x;

return 0;
}

Usually there's no good reason to use pointers to members.

Any pointer to member can be represented instead as a pointer to a
function, like

struct MyClass{ int x; };

int& xMember( MyClass& o ) { return o.x; }

int main()
{
int (*pfx)( MyClass& ) = &xMember;
}

Well, at least I think the "any" holds. Pointer to members are so
rarely used that I can't think of an example where the "any" doesn't
hold. Although such an example may exist.
 
P

Phlip

Amadeus said:
I saw somewhere some code that in a simplified version boils down to the
example below. Could someone explain when one would define a pointer as
that in main() below, and why? Pointers to documents that explain this
will do. Thanks!

Google [C++ faq member pointer]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top