I have seen an example. in that example there is a line like x->*y
. what is it? ->*
. I am new in C++ programming and I don't have much knowledge about operator. Can anybody describe it?
Asked
Active
Viewed 721 times
0
-
2Not hard at all to check: http://en.cppreference.com/w/cpp/language/operator_precedence. Oh, and it isn't part of C. – chris Feb 22 '14 at 06:10
-
1It's not one operator. It's the `->` operator followed by the `*` operator. – Barmar Feb 22 '14 at 06:12
-
1@Barmar, It sure is in C++, and I don't think you can have that syntax in C. – chris Feb 22 '14 at 06:15
-
@chris I have read that and i found some operator like `->`, `*` and `->*`. So, i am getting confused. Is it combination of two operator `->` and `*` or it just a single operator `->*` (Pointer to member)? – Shell Feb 22 '14 at 06:16
-
@Nimesh, It's a single operator. Those are three separate things. Tons of resources on each of them on Google and in books. – chris Feb 22 '14 at 06:17
-
https://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fcplr034.htm – Barmar Feb 22 '14 at 06:18
-
http://msdn.microsoft.com/en-us/library/k8336763.aspx – Barmar Feb 22 '14 at 06:19
-
2@Felix Same with [What are the Pointer-to-Member ->* and .* Operators in C++?](https://stackoverflow.com/questions/6586205/what-are-the-pointer-to-member-and-operators-in-c) Not sure why duplicates get upvoted. – Feb 22 '14 at 06:29
-
@remyabel the only problem with the answers in these dupes is they're all seemingly centric on pointer to member *functions*, failing to fully cover that the operator is useful for pointer to member *everything*. For some reason member *variables* isn't covered well, and in most cases at-all. No clue why. [**See it live**](http://ideone.com/10wnBm). – WhozCraig Feb 22 '14 at 06:36
-
@FelixKling The link posted by remyabel is earlier. – Jason C Feb 22 '14 at 06:40
2 Answers
4
It's called "pointer to member of pointer" and is one of the "pointer to member" type operators (in addition to .*
, "pointer to member of object").
You can use it when you take the address of a member variable or function of a class, then you want to access that variable or call that function on an instance of that class, given a pointer to the instance (like a vanilla data or function pointer, but to a class member).
Here is an example using function pointers:
#include <cstdio>
using namespace std;
class Example {
public:
Example (int value) : value_(value) { }
void printa (const char *s) { printf("A %i %s\n", value_, s); }
void printb (const char *s) { printf("B %i %s\n", value_, s); }
private:
int value_;
};
// print_member_ptr can point to any member of Example that
// takes const char * and returns void.
typedef void (Example::* print_member_ptr) (const char *);
int main () {
print_member_ptr ptr;
Example x(1), y(2), *p = new Example(3), *q = new Example(4);
ptr = &Example::printa;
// .*ptr and ->*ptr will call printa
(x.*ptr)("hello");
(y.*ptr)("hello");
(p->*ptr)("hello");
(q->*ptr)("hello");
ptr = &Example::printb;
// now .*ptr and ->*ptr will call printb
(x.*ptr)("again");
(y.*ptr)("again");
(p->*ptr)("again");
(q->*ptr)("again");
}
The output is:
A 1 hello
A 2 hello
A 3 hello
A 4 hello
B 1 again
B 2 again
B 3 again
B 4 again
See http://en.cppreference.com/w/cpp/language/operator_member_access for more details.

Jason C
- 38,729
- 14
- 126
- 182
2
It's a Member pointed to by b of object pointed to by a.
Syntax
a->*b
As member of K
R &operator ->*(K a, S b);
Outside class definitions
R &K::operator ->*(S b);
See more at Wikipedia.