Friday, January 23, 2009

Method pointer G++

Look at this code:

#include <iostream>
using namespace std;


class A
{
public:
void Test(int i)
{
mc(&A::ma, i);
mc(mb, i);
};
protected:
void ma(int i) { cout << "A::ma(" << i << ") has been called" << endl;};
void mb(int i) { cout << "A::mb(" << i << ") has been called" << endl;};
void mc(void (A::*m)(int), int i) { cout << "A::mc "; (this->*m)(i);};
};


int main()
{
A a;
a.Test(5);
}


In Visual Studio it compiles fine. But in g++ the error occurs:
 
FPointer.cpp: In member function ‘void A::Test(int)’:
FPointer.cpp:11: error: no matching function for call to ‘A::mc(<unresolved overloaded function type>, int&)’
FPointer.cpp:16: note: candidates are: void A::mc(void (A::*)(int), int)


That's because g++ compiler expects strict writing. Compare lines 10 and 11 to solve this error.

No comments:

Post a Comment