3

I came across code today that looked like:

result = (this->*(*c))(&param)

The main part that confuses me is the this->*(*c) What does it mean to have the asterisk operators between the arrow (->) and the name of the variable we're accessing (c).

turbulencetoo
  • 3,447
  • 1
  • 27
  • 50
  • If anyone has ideas on how to edit the title such that people with the same question as me could find this question easier, please edit. – turbulencetoo Apr 10 '15 at 18:58
  • 5
    `->*` is one operator. `*` is another. It's just a sequence of composed subexpressions. – Kerrek SB Apr 10 '15 at 18:58
  • Perhaps the arrow is throwing me off; what would this code look like if composed of only `.` and `*`? Is `((* this).*(*c))` valid code? – turbulencetoo Apr 10 '15 at 19:00
  • @turbulencetoo, `.*` is one operator, just like `->*`, and has the same relation as `.` to `->` for what it can be called on. – chris Apr 10 '15 at 19:02
  • It's a pointer-to-member See e.g. [this](http://www.studytonight.com/cpp/pointer-to-members.php). – TonyK Apr 10 '15 at 19:04
  • A related SO question available [here].(http://stackoverflow.com/questions/2548555/dot-asterisk-operator-in-c) Is this also a concept in C, or is it C++ only? – turbulencetoo Apr 10 '15 at 19:05
  • @turbulencetoo, C doesn't have pointers to members, so no. A quick way to check is [this Wikipedia table](http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Member_and_pointer_operators). – chris Apr 10 '15 at 19:07
  • Actually, `c` looks like a pointer to a pointer to member function, which is very rare to see. Maybe `c` is part of an array of function pointers and the user is getting the element from the array using the addresses of the elements. – David G Apr 10 '15 at 19:38

2 Answers2

4

What you have here is an operator which you don't see very often.

->* is a single operator. It is the pointer-based counterpart to .* and is a member access operator.

It is used if you have an object to use a member on (e.g. a function), but don't know the concrete member (it's stored in a variable).

Let's split it up:

this      // object to work on
->*       // member access operator
(*c)      // dereference pointer pointing to member function (c is a pointer-to-pointer)
(&param)  // call member function stored in c on this passing &param to the function

See also: http://en.cppreference.com/w/cpp/language/operator_member_access

Edit: This post also contains a good explaination on what is happening here: https://stackoverflow.com/a/6586248/1314789

Community
  • 1
  • 1
Excelcius
  • 1,680
  • 1
  • 14
  • 31
  • 1
    `c` looks to me to be a *pointer-to-pointer* to member function. Otherwise the syntax `(this->*c)(&param)` would be enough. – David G Apr 10 '15 at 19:37
  • Yes I though so too, but didn't mention it. I will add it to the answer, thanks. – Excelcius Apr 10 '15 at 20:06
0

The parse tree for the expression is this:

                         =
                  /            \
               result     function call
                            /       \
                          ->*        &
                         /   \       |
                       this   *    param
                              |
                              c

The parentheses are necessary because of boring grammar reasons.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084