如何使用函数指针调用对象的方法?
How to call object's methods using a function pointer?
我想从 class 'Caller' 中调用 classes 'A' 和 'B' 的一些方法。我需要使用函数指针,因为我想调用不同的方法。
我的方法被调用了,但是当我试图从它访问一个成员变量时,我的程序崩溃了 ('program.exe has stopped working')。
怎么会这样?
#include <iostream>
using namespace std;
template <class T>
class Caller
{
typedef void (T::*myFunc)(int);
public:
Caller(T* obj, myFunc fp)
{
f = fp;
}
void invoke(int foobar)
{
(o->*f)(foobar);
}
private:
myFunc f;
T* o;
};
class A
{
public:
A() : n(0) {}
void foo(int bar)
{
cout << "A::foo called (bar = " << bar << ", n = " << n << ")" << endl; // the crash occurs here, and 'this' equals 0 at this point
}
void setNum(int num)
{
n = num;
}
private:
int n;
};
class B
{
public:
B() : n(0) {}
void fooo(int bar)
{
cout << "B::fooo called (bar = " << bar << ", n = " << n << ")" << endl; // same here if I call B::fooo first
}
void setNum(int num)
{
n = num;
}
private:
int n;
};
int main()
{
A myA;
B myB;
myA.setNum(128);
myB.setNum(256);
Caller<A> cA(&myA, &A::foo);
Caller<B> cB(&myB, &B::fooo);
cA.invoke(10);
cB.invoke(20);
return 0;
}
提前致谢。
编辑:我使用 VS2017,我可以构建我的程序而不会出现任何编译器错误。
My method gets called, but when I try to access a member variable from it, my program crashes ...
因为您忘记将传递的 obj
分配给 Caller
中的 o
指针:
template <class T>
class Caller
{
typedef void (T::*myFunc)(int);
public:
Caller(T* obj, myFunc fp)
{
o = obj; // << == you need this!
f = fp;
}
void invoke(int foobar)
{
(o->*f)(foobar);
}
private:
myFunc f;
T* o;
};
此外,一般来说最好使用member initializer lists:
Caller::Caller(T* obj, myFunc fp) : o(obj), f(fp)
{
}
我想从 class 'Caller' 中调用 classes 'A' 和 'B' 的一些方法。我需要使用函数指针,因为我想调用不同的方法。
我的方法被调用了,但是当我试图从它访问一个成员变量时,我的程序崩溃了 ('program.exe has stopped working')。
怎么会这样?
#include <iostream>
using namespace std;
template <class T>
class Caller
{
typedef void (T::*myFunc)(int);
public:
Caller(T* obj, myFunc fp)
{
f = fp;
}
void invoke(int foobar)
{
(o->*f)(foobar);
}
private:
myFunc f;
T* o;
};
class A
{
public:
A() : n(0) {}
void foo(int bar)
{
cout << "A::foo called (bar = " << bar << ", n = " << n << ")" << endl; // the crash occurs here, and 'this' equals 0 at this point
}
void setNum(int num)
{
n = num;
}
private:
int n;
};
class B
{
public:
B() : n(0) {}
void fooo(int bar)
{
cout << "B::fooo called (bar = " << bar << ", n = " << n << ")" << endl; // same here if I call B::fooo first
}
void setNum(int num)
{
n = num;
}
private:
int n;
};
int main()
{
A myA;
B myB;
myA.setNum(128);
myB.setNum(256);
Caller<A> cA(&myA, &A::foo);
Caller<B> cB(&myB, &B::fooo);
cA.invoke(10);
cB.invoke(20);
return 0;
}
提前致谢。
编辑:我使用 VS2017,我可以构建我的程序而不会出现任何编译器错误。
My method gets called, but when I try to access a member variable from it, my program crashes ...
因为您忘记将传递的 obj
分配给 Caller
中的 o
指针:
template <class T>
class Caller
{
typedef void (T::*myFunc)(int);
public:
Caller(T* obj, myFunc fp)
{
o = obj; // << == you need this!
f = fp;
}
void invoke(int foobar)
{
(o->*f)(foobar);
}
private:
myFunc f;
T* o;
};
此外,一般来说最好使用member initializer lists:
Caller::Caller(T* obj, myFunc fp) : o(obj), f(fp)
{
}