# C++ operator '-\>\*'

**URL:** <https://boards.straightdope.com/t/c-operator/448428>\
**Category:** Factual Questions\
**Created:** [May 7, 2008, 2:14pm UTC](https://boards.straightdope.com/t/c-operator/448428 "2008-05-07T14:14:48Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Dubinsky](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@Alex\_Dubinsky](https://boards.straightdope.com/u/Alex_Dubinsky)\
**Post date:** [May 7, 2008, 2:14pm UTC](https://boards.straightdope.com/t/c-operator/448428/1 "2008-05-07T14:14:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [May 7, 2008, 2:16pm UTC](https://boards.straightdope.com/t/c-operator/448428/2 "2008-05-07T14:16:29Z")

</div>

Never heard of that one. Where’d you see it?

---

<div class="post-metadata">

**Author:** ![Alex\_Dubinsky](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@Alex\_Dubinsky](https://boards.straightdope.com/u/Alex_Dubinsky)\
**Post date:** [May 7, 2008, 2:18pm UTC](https://boards.straightdope.com/t/c-operator/448428/3 "2008-05-07T14:18:31Z")

</div>

> **[Operators in C and C++](https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B)**
>
> This is a list of operators in the C and C++ programming languages. All the operators (except typeof) listed exist in C++; the column "Included in C", states whether an operator is also present in C. Note that C does not support operator overloading.
> When not overloaded, for the operators &&, ||, and , (the comma operator), there is a sequence point after the evaluation of the first operand.
> C++ also contains the type conversion operators const\_cast, static\_cast, dynamic\_cast, and reinterpret...

wiki calls it “Member by Pointer Indirection”

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [May 7, 2008, 2:24pm UTC](https://boards.straightdope.com/t/c-operator/448428/4 "2008-05-07T14:24:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Alex\_Dubinsky](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@Alex\_Dubinsky](https://boards.straightdope.com/u/Alex_Dubinsky)\
**Post date:** [May 7, 2008, 2:40pm UTC](https://boards.straightdope.com/t/c-operator/448428/5 "2008-05-07T14:40:52Z")

</div>

[QUOTE=ultrafilter]  
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.  
[/QUOTE]

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.

---

<div class="post-metadata">

**Author:** ![UncleRojelio](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/unclerojelio/32/3160_2.png) [@UncleRojelio](https://boards.straightdope.com/u/UncleRojelio)\
**Post date:** [May 7, 2008, 2:44pm UTC](https://boards.straightdope.com/t/c-operator/448428/6 "2008-05-07T14:44:53Z")

</div>

[QUOTE=ultrafilter]  
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.  
[/QUOTE]

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.

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [May 7, 2008, 2:46pm UTC](https://boards.straightdope.com/t/c-operator/448428/7 "2008-05-07T14:46:43Z")

</div>

[QUOTE=Alex\_Dubinsky]  
So you’d guess it’s be the same thing as \*a-\>b.  
[/QUOTE]

Not exactly. What I’m thinking is something like this:

```auto

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.

---

<div class="post-metadata">

**Author:** ![Alex\_Dubinsky](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@Alex\_Dubinsky](https://boards.straightdope.com/u/Alex_Dubinsky)\
**Post date:** [May 7, 2008, 2:54pm UTC](https://boards.straightdope.com/t/c-operator/448428/8 "2008-05-07T14:54:45Z")

</div>

**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.

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [May 7, 2008, 3:03pm UTC](https://boards.straightdope.com/t/c-operator/448428/9 "2008-05-07T15:03:14Z")

</div>

I found [this article](http://msdn.microsoft.com/en-us/library/k8336763(VS.80).aspx) 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.

---

<div class="post-metadata">

**Author:** ![Rysto](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@Rysto](https://boards.straightdope.com/u/Rysto)\
**Post date:** [May 7, 2008, 3:33pm UTC](https://boards.straightdope.com/t/c-operator/448428/10 "2008-05-07T15:33:11Z")

</div>

Ugh, pointers to members. I’d forgotten such things exist. It doesn’t do what you said, **ultafilter**. Maybe this will clarify:

```auto

//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.

---

<div class="post-metadata">

**Author:** ![UncleRojelio](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/unclerojelio/32/3160_2.png) [@UncleRojelio](https://boards.straightdope.com/u/UncleRojelio)\
**Post date:** [May 7, 2008, 3:37pm UTC](https://boards.straightdope.com/t/c-operator/448428/11 "2008-05-07T15:37:33Z")

</div>

[QUOTE=Alex\_Dubinsky]  
**UncleRojelio** , you’re talking about the ‘-\>’ operator, not ‘-\>_’ or its apparent partner, '._’  
[/QUOTE]

Crap. Sorry. I didn’t read the OP closely enough. Had I, my (useless) answer would have been, “Hell if I know”.

---

<div class="post-metadata">

**Author:** ![Bytegeist](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@Bytegeist](https://boards.straightdope.com/u/Bytegeist)\
**Post date:** [May 7, 2008, 3:52pm UTC](https://boards.straightdope.com/t/c-operator/448428/12 "2008-05-07T15:52:55Z")

</div>

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.

[QUOTE=ultrafilter]

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.

[/QUOTE]

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](http://www.codeproject.com/KB/cpp/FastDelegate.aspx).

---

<div class="post-metadata">

**Author:** ![Alex\_Dubinsky](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@Alex\_Dubinsky](https://boards.straightdope.com/u/Alex_Dubinsky)\
**Post date:** [May 7, 2008, 4:12pm UTC](https://boards.straightdope.com/t/c-operator/448428/13 "2008-05-07T16:12:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [May 7, 2008, 7:14pm UTC](https://boards.straightdope.com/t/c-operator/448428/14 "2008-05-07T19:14:46Z")

</div>

[QUOTE=Alex\_Dubinsky]  
But damn, I’m still impressed there was a whole operator in C++ I had no idea existed.  
[/QUOTE]

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.

---

<div class="post-metadata">

**Author:** ![Cerowyn](https://avatars.discourse-cdn.com/v4/letter/c/82dd89/32.png) [@Cerowyn](https://boards.straightdope.com/u/Cerowyn)\
**Post date:** [May 7, 2008, 7:15pm UTC](https://boards.straightdope.com/t/c-operator/448428/15 "2008-05-07T19:15:21Z")

</div>

[QUOTE=Alex\_Dubinsky]  
But damn, I’m still impressed there was a whole operator in C++ I had no idea existed.  
[/QUOTE]  
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.

---

<div class="post-metadata">

**Author:** ![Indistinguishable](https://avatars.discourse-cdn.com/v4/letter/i/90ced4/32.png) [@Indistinguishable](https://boards.straightdope.com/u/Indistinguishable)\
**Post date:** [May 7, 2008, 7:19pm UTC](https://boards.straightdope.com/t/c-operator/448428/16 "2008-05-07T19:19:46Z")

</div>

Count me in as another one who had no idea this one (or, rather, these two) existed.

---

<div class="post-metadata">

**Author:** ![alterego](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@alterego](https://boards.straightdope.com/u/alterego)\
**Post date:** [May 7, 2008, 7:35pm UTC](https://boards.straightdope.com/t/c-operator/448428/17 "2008-05-07T19:35:57Z")

</div>

[QUOTE=Alex\_Dubinsky]  
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.  
[/QUOTE]

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](http://google.com/codesearch?hl=en&lr=&q=%5C-%5C%3E%5C*+lang%3Ac%2B%2B+pointer+member&sbtn=Search&num=100r)

The syntax is then documented in the comments.

---

<div class="post-metadata">

**Author:** ![Bytegeist](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@Bytegeist](https://boards.straightdope.com/u/Bytegeist)\
**Post date:** [May 7, 2008, 7:44pm UTC](https://boards.straightdope.com/t/c-operator/448428/18 "2008-05-07T19:44:19Z")

</div>

[QUOTE=Alex\_Dubinsky]

so basically -\>\* overcomes the problem of taking a pointer to a member functions.

[/quote]

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_.

> [@](#):
>
> To me I think the biggest use will be as a cool operator to overload.

Knock yourself out.

Oddly enough, the ‘-\>_’ operator can be overridden, but not its cousin '._’ .
