What does it mean? Google is completely useless. I stumbled across it in a list of operators, and was really surprised I’ve never seen it before.
Never heard of that one. Where’d you see it?
wiki calls it “Member by Pointer Indirection”
I don’t have time to check right now, but I’m guessing that if you have a pointer a to an object with a member pointer b, a->*b will get you whatever b points to.
So you’d guess it’s be the same thing as *a->b. Seems pretty lame. (Although it would’ve been really good if *a->b meant (*a)->b in c++ and then a->*b would make sense). Also, from the wiki article, seems it has lower precedence than either -> or *, which is rather surprising. *a->b->*c->d means (a->b)->(c->d). wtf.
Close, If you have a pointer to an object a, a->b gets you the member b. It works the same as the ‘.’ operator, as in, if a is an object, a.b is the member b.
Not exactly. What I’m thinking is something like this:
class A
{
public:
int* b;
};
A* a = new A;
a->b = new int;
a->*b = 3;
If this is what it actually does, it’s more for convenience than anything else.
UncleRojelio, you’re talking about the ‘->’ operator, not ‘->’ or its apparent partner, '.’
ultrafilter, that’s exactly what *a->b means.
guys, let’s give this a minute.
I found this article at MSDN that gives examples of the ->* and .* operators in use. It looks like it does basically what I said (and is not equivalent to *a->b), although it’s still not entirely clear why it exists.
Ugh, pointers to members. I’d forgotten such things exist. It doesn’t do what you said, ultafilter. Maybe this will clarify:
//says that "pmd is a pointer to an integer field of the class Testpm".
//Note that we don't specify *which* field, only that it's an integer and a member of Testpm.
int Testpm::*pmd;
//says that pmd points to the m_num field of Testpm.
pmd = &Testpm::m_num;
Testpm * a = new Testpm;
Testpm * b = new Testpm;
a->*pmd = 5; //set field pointed to by pmd in object pointed to by a to 5
b->*pmd = 1; //set field pointed to by pmd in object pointed to by b to 5
cout << a->m_num << endl; // prints 5
cout << b->m_num << endl; // prints 1
// pmd now points to m_num2
pmd = &Testpm::m_num2;
a->*pmd = -1; //set field pointed to by pmd in object pointed to by a to -1
b->*pmd = 0; //set field pointed to by pmd in object pointed to by b to 0
cout << a->m_num2 << endl; // prints -1
cout << b->m_num2 << endl; // prints 0
Does that clarify what it’s for? I don’t think that pointers to members are ever really used; you’re much better off using OOP to accomplish the same kind of thing.
Crap. Sorry. I didn’t read the OP closely enough. Had I, my (useless) answer would have been, “Hell if I know”.
I have seen this operator used as a sort of “computed goto” for calling object methods, to implement delegates.
Suppose you have a class with two or more methods having the exact same signature (same number of arguments, same argument types, same return type). You can get a pointer to one of the methods — then at some later time, call the method indirectly through that pointer.
You could also form an array of such pointers, if you wanted to dispatch over the methods using an integer index.
It’s a useful bit of syntax because calling an object method is different from calling an ordinary function. A method call has a hidden “this” pointer as its first argument, which the ‘->’ and '.’ operators provide. You don’t get that with the classical ‘->’ or ‘.’ operators.
There’s some useful description of the concepts on this page.
Aha,
so basically ->* overcomes the problem of taking a pointer to a member functions. For fields it also provides a bit of type checking, but that seems largely useless.
To me I think the biggest use will be as a cool operator to overload.
But damn, I’m still impressed there was a whole operator in C++ I had no idea existed.
Seriously. After 10 years of programming C++, you’d think I’d know the freaking syntax by now.
Thanks for the correction, Rysto. I still don’t quite get why it’s in the language, though.
As was I. I was already to claim that it was just a consequence of the -> and * operators, but then I learned that it is an ISO standard C++ operator that can be overloaded.
Count me in as another one who had no idea this one (or, rather, these two) existed.
Google is not completely useless here. Using Google code search, you can search for the syntax, and the names of the individual components in the syntax (which are easily discoverable).
http://google.com/codesearch?hl=en&lr=&q=\-\>\*+lang%3Ac%2B%2B+pointer+member&sbtn=Search&num=100r
The syntax is then documented in the comments.
Right.
Well, I’m not sure there’s a problem to be overcome. Given that the language lets you generate a pointer to a member function, the ‘->*’ operator is what lets you actually call the function. Without this operator, the pointer would be rather, you know, pointless.
Knock yourself out.
Oddly enough, the ‘->’ operator can be overridden, but not its cousin '.’ .