简体   繁体   中英

Return a pointer to class member function

The following Problem drives me nuts, though it doesn't seem very odd:

class Foo;

// This is the location of the first error code
//        ↓
int (Foo::*)(int) getPointer()
{
    return 0;
}

GCC gives me:

error: expected unqualified-id before ')' token
error: expected initializer before 'getPointer'

PS: I compile with -std=c++11

int ( Foo::* ( getPointer() ) )();

That being said, remember you can use typedef . For function pointers, it usually improves the overall readability:

typedef int ( Foo::* TypeName )();

TypeName getPointer();

use a typedef, like:

class Foo;

typedef int (Foo::*POINTER)(int);

POINTER getPointer()
{
    return 0;
}

For more reasoning here goes: http://www.parashift.com/c++-faq/typedef-for-ptr-to-memfn.html

Looks like you are trying to use function pointers, but not giving a name to it :P

Use this:

int (Foo::*myPointer)(int) getPointer()
{
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM