3

I've never used it before and just stumbled upon it in an article... I thought it would be the equivalent to *x->y but apparently it isn't.

Here's what I tried, and gave me an error:

struct cake {
 int * yogurt;
} * pie;

int main(void) {
 pie = new cake;
 pie->yogurt = new int;
 return pie->*yogurt = 4;
}
slartibartfast
  • 4,348
  • 5
  • 31
  • 46
  • It is primarily used for method pointers and function member pointers. – Pubby Oct 07 '11 at 01:02
  • Ok, but can someone actually explain its use and/or provide an example code that uses it correctly? – slartibartfast Oct 07 '11 at 01:04
  • possible duplicate of [What are the Pointer-to-Member ->\* and .\* Operators in C++?](http://stackoverflow.com/questions/6586205/what-are-the-pointer-to-member-and-operators-in-c) (That one is dated earlier than this). – Jason C Feb 22 '14 at 06:38

4 Answers4

4

Its used when you have pointers to member functions.

When you have a pointer to a function of a class, you call it in much the same way you would call any member function

object.membername( ... )

or

objectptr->membername( ... )

but when you have a member function pointer, an extra * is needed after the . or -> in order that the compiler understand that what comes next is a variable, not the actual name of the function to call.

Here's an example of how its used.

class Duck
{
public:

  void quack() { cout << "quack" << endl; }
  void waddle() { cout << "waddle" << endl; }
};

typedef void (Duck::*ActionPointer)();

ActionPointer myaction = &Duck::quack;

void takeDuckAction()
{    
    Duck myduck;
    Duck *myduckptr = &myduck;

    (myduck.*myaction)();
    (myduckptr->*myaction)();
}
MerickOWA
  • 7,453
  • 1
  • 35
  • 56
  • I get it, so it's used along with the `::*` for nonstatic member function pointers. Thanks. – slartibartfast Oct 07 '11 at 01:14
  • @myrkos yep, updated the answer a bit to make that more clear – MerickOWA Oct 07 '11 at 01:15
  • @Ben, ok, but when would that actually be useful rather than plain pointer dereferencing? – slartibartfast Oct 07 '11 at 01:41
  • @myrkos: A member pointer is used when the target object is chosen long after the pointer is created. For example, you could make a sort function that takes an array of structures and a pointer-to-member, and uses that to extract the sort field from each element. A functor is generally more powerful than a pointer-to-member, but not always as efficient. – Ben Voigt Oct 07 '11 at 01:45
3

It defines a pointer to a member.

In an expression containing the –>* operator, the first operand must be of the type "pointer to the class type" of the type specified in the second operand, or it must be of a type unambiguously derived from that class. MSDN

2

Pointer-to-Member Operators: .* and ->*

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

The .* and ->* operators will point to member functions of a class or structure. The code below will show a simple example of how to use the .* operator, if you change the line: Value funcPtr = &Foo::One; to Value funcPtr = &Foo::Two; the result displayed will change to 1000 since that function is inValue*2

for example Taken From Here:

#include <iostream>
#include <stdlib.h>

class Foo { 
  public: 
    double One( long inVal ) { return inVal*1; }
    double Two( long inVal ) { return inVal*2; }
}; 

typedef double (Foo::*Value)(long inVal); 

int main( int argc, char **argv ) { 
  Value funcPtr = &Foo::One; 

  Foo foo;

  double result = (foo.*funcPtr)(500); 
  std::cout << result << std::endl;
  system("pause");
  return 0; 
} 
Serdalis
  • 10,296
  • 2
  • 38
  • 58