1

I am new to function pointers and I would like your help. I am having a method:

int test3(int i)
{
    return i;
}

Then in another method(not main) I do:

int (*pTest3)(int) = test3;

From the examples that I have read this seems ok. However, I get a compile time error:

testFile.cpp:277:25: error: argument of type ‘int ({anonymous}::CheckingConsumer::)(int)’ does not match ‘int (*)(int)’

I do not understand what is wrong. Any help would be appreciated.

Thanks a lot.

7
  • 3
    Is that function a class member function? The error seems to indicate it is.... If you want to make a member function pointer it's a bit different. Commented Aug 9, 2012 at 14:59
  • This is correct syntax, there must be some other things going on. What is your compiler? In what scoope is this code (ie. you can't use class functions here)? Commented Aug 9, 2012 at 15:01
  • 1
    There's nothing wrong with it. Look from the size of your file (at least 277 lines) that there maybe something else which is causing this (CheckingConsumer ?) that you haven't included above. Commented Aug 9, 2012 at 15:04
  • 2
    It would be helpful if in your questions you could include a complete but minimal example showing your problem. Here we have to read between the lines a lot to infer that test3 is really a member of CheckingConsumer which makes the question harder to answer than it needs to be. Commented Aug 9, 2012 at 15:04
  • 1
    @Flexo: Exactly: What is really happening is nothing to do with the Op's example code. Commented Aug 13, 2012 at 13:05

2 Answers 2

7

Your test3 is a member function of a struct or a class. Class member functions have a hidden this parameter passed into them and so cannot be used with plain function pointers. You need to either declare the function as static or move it outside the struct/class, so that it no longer has a hidden this parameter, or use a class method pointer instead of a function pointer:

// static class method:
class X
{
    static int test3(int i)
    {
        ...
    }
};

// Non-class method, at global scope
int test3(int i)
{
    ...
}

// Class method pointer
class X
{
    int test3(int i)
    {
        ...
    }
};

// Create the method pointer
int (X::*pTest3) = &X::test3;
X *obj;
// Call the method pointer on an object
(obj ->* pTest3)(42);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for the reply. It worked using the static keyword. However, does this affect the program at all ?
@user1491620 static makes it not work with an instance of your class, which significantly changes the meaning.
0

Your method test3 seems to be an instance method. Later on you define pTest3 as function pointer, not as member function pointer.

Main difference between simple pointers and member pointers is that using the member pointer requires an instance of the object. An instance of the object tells what object should be processed and the value of the pointer tells what data field of the object should be used or what member function should be called. Value of the member pointer is conceptually equivalent to the offset from the beginning of the object to its member.

Declaring the member pointer using typedef:

typedef int (SomeClass::*MyMethodPtr)(int i);
MyMethodPtr ptr3 = SomeClass::test3;

Now using this pointer:

class SomeClass *pab = &ab;
int ret_value = (pab->*ptr3)(4);

Note that the instance of the class is used. There is other important point about the member pointers. They are implemented as structs that contain inside from 2 to 5 simple pointers and offsets depending on the compiler and other aspects like multiple inheritance, presence of vitrual base classes, etc.

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.