Jump to content
  • Advertisement
Sign in to follow this  
Koen

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

This topic is 2904 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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)(); //ok
typedef 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!

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :-)

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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;
}



Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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; //ok
TFreeFunctionPointer ffp2 = SomeFreeFunction; //ok


typedef void (TObject::*TMethodPointer)();

TMethodPointer mp1 = &TObject::SomeMethod; //ok
//TMethodPointer mp2 = TObject::SomeMethod; //error!!


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!