Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Koen   Members   -  Reputation: 546

Like
0Likes
Like

Posted 05 July 2010 - 03:20 AM

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!

Sponsor:

#2 Zipster   Crossbones+   -  Reputation: 1088

Like
0Likes
Like

Posted 05 July 2010 - 05:53 AM

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

#3 DaBono   Members   -  Reputation: 1144

Like
0Likes
Like

Posted 05 July 2010 - 06:15 AM

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.

#4 Koen   Members   -  Reputation: 546

Like
0Likes
Like

Posted 06 July 2010 - 05:48 AM

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 :-)

#5 Zipster   Crossbones+   -  Reputation: 1088

Like
0Likes
Like

Posted 06 July 2010 - 06:46 AM

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 :)

#6 hdanw   Members   -  Reputation: 100

Like
0Likes
Like

Posted 07 July 2010 - 03:36 PM

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;
}




#7 iMalc   Crossbones+   -  Reputation: 2397

Like
0Likes
Like

Posted 07 July 2010 - 09:36 PM

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.

#8 Koen   Members   -  Reputation: 546

Like
0Likes
Like

Posted 08 July 2010 - 12:50 AM

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!!







Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS