3

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

3
  • 1
    For God's sake use typedefs Commented Nov 13, 2013 at 19:00
  • 1
    @Slava Totally agree, but it's also nice to understand how it works. Commented Nov 13, 2013 at 19:13
  • @Slava I cannot, since the return type is dependent of a deduced template parameter and you cannot typedef between 'template<>' and 'getPointer()' Commented Nov 13, 2013 at 19:35

3 Answers 3

4
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();
Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

It is usually bad idea to use all uppercase identifiers.
It usually depends on the coding standard your company decided to adopt.
to use uppercase identifiers for preprocessor does not seem to be a company coding standard, but used everywhere.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.