[C++] pointer/reference to function/method

Started by
6 comments, last by Koen 13 years, 9 months ago
Apparently 'function references' exist in C++. The C++ Programming Language doesn't seem to explicitly state this, though. In Visual C++ 2005 and Comeau's online compiler, the following code compiles:
void DummyFunction() {}typedef void (*TFunctionPointer)();typedef void (&TFunctionReference)();int main()  {  TFunctionPointer fp1 = DummyFunction;  TFunctionPointer fp2 = &DummyFunction;  TFunctionReference fr1 = DummyFunction;  TFunctionReference fr2 = *DummyFunction;  fp1();  fp2();  fr1();  (&fr1)();  fr2();  return 0;  }

You can see that you don't need the &-operator to get a function pointer. The C++ Programming Language says:.
Quote:The C++ Programming Language, 7.7 Pointer to Function
... That is, dereferencing of a pointer to function using * is optional. Similarly, using & to get the address of a function is optional...

Now I want to do the same thing for pointers to methods. Unfortunately, a 'reference to method' doesn't seem to be possible. The following code doesn't compile:
struct DummyStruct  {  void DummyMethod() {}  };typedef void (DummyStruct::*TMethodPointer)(); //oktypedef void (DummyStruct::&TMethodReference)(); //error

There is nothing that cannot be done with method pointers, but it would be nice to be able to have a method reference as parameter (for the same reason it's nice to be able to use normal references instead of pointers). Anybody any clue whether this is possible? Thanks!
Advertisement
A reference in C++ is really just a fancy alias. I've never personally attempted function references, but my guess is that they don't work with member functions because you also need to have an instance to act upon for a member function call to be "complete". Otherwise it can't be properly aliased by the compiler. However a reference to a std::function is surely possible (since you can bind an instance).
As Zipster says, pointers to member functions are different since they need a instance (i.e. this-pointer) to act upon. If you just use the classes/structs for encapsulation and don't need to use an instance, you can make the method static and use them like ordinary functions.

For the real member function pointers, I gladly direct you to the interwebz.
I don't think the "you need an object"-argument is valid. If it were, it would also be valid for pointers-to-method. And those certainly are legal. But thanks for your guesses :-)
Quote:Original post by Koen
I don't think the "you need an object"-argument is valid. If it were, it would also be valid for pointers-to-method.

I don't see why. A free function pointer and a member function pointer both store essentially the same thing, the address of some code somewhere. The only difference is that member functions have a unique calling convention that requires an instance to be supplied when they're called. References on the other hand are aliases, and while they might behave like pointers (and are often implemented in compilers as such), they're not the same thing. A member function pointer in and of itself can't be called, which is probably why you can't create a reference to one. Again I've never actually tried, but you've already shown it doesn't work so I'm thinking this sounds like a plausible explanation :)
Pointers to methods do exist.

They do need to exist.

Since an instance does not neccessarily exist when a pointer does, you have to declare the member static. So it's always there.

This kind of sucks if you want to operate on instance data. But then you'd probably just use the built in pointer that is store in the vtable for that instance to invoke the function. Then you can just store a pointer to the object instance instead of a pointer to one of it's methods.

But It kind of doen't suck when you have a well suited purpose for this.


#include "stdafx.h"

class SomeClass {
public:
static void SomeMember ();
};

void SomeClass::SomeMember () {
// do something
}

typedef void (* fSomeMember)();

void main () {
fSomeMember myFunctionPointer;
myFunctionPointer = SomeClass::SomeMember;
}



I use function references. In fact the last thread I started on here as about them.
No idea about member function references though. I've never even had the need to write code with member function pointers.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Quote:Original post by Zipster
The only difference is that member functions have a unique calling convention that requires an instance to be supplied when they're called. References on the other hand are aliases, and while they might behave like pointers (and are often implemented in compilers as such), they're not the same thing. A member function pointer in and of itself can't be called, which is probably why you can't create a reference to one.

Thinking about this further, I see how this makes sense somehow. I still don't really understand, though :)
I just discovered that in VS2005 you have to use the &-operator to get a method pointer. Which probably adds to the there-is-a-difference case. So:
typedef void (*TFreeFunctionPointer)();TFreeFunctionPointer ffp1 = &SomeFreeFunction; //okTFreeFunctionPointer ffp2 = SomeFreeFunction; //oktypedef void (TObject::*TMethodPointer)();TMethodPointer mp1 = &TObject::SomeMethod; //ok//TMethodPointer mp2 = TObject::SomeMethod; //error!!

This topic is closed to new replies.

Advertisement