指向成员函数语法的指针
Pointer to member function syntax
我正试图围绕指向成员函数的指针进行思考,并被这个例子所困:
#include <iostream>
class TestClass {
public:
void TestMethod(int, int) const;
};
void TestClass::TestMethod(int x, int y) const {
std::cout << x << " + " << y << " = " << x + y << std::endl;
}
int main() {
void (TestClass::*func)(int, int) const;
func = TestClass::TestMethod;
TestClass tc;
tc.func(10, 20);
return 0;
}
我认为代码应该做什么:
- 在 main 的第一行我声明了一个指向
class TestClass
的成员函数的指针,其中 returns nothing/takes 两个 int
并且被声明为 const
,称为 func.
- 第二行将成员函数
TestMethod
(属于 class TestClass
)分配给满足这些条件的 func
。
- 第四行创建一个名为 tc 的
TestClass
对象。
- 第五行尝试在
TestClass
对象tc
. 上调用func
指向的成员函数
我遇到两个编译错误:
而不是将 TestClass::TestMethod
分配给 func
。编译器尝试调用 TestClass::TestMethod
(即使它不是 static
,因此抛出错误):
testPointerToMemberFunc.cpp:14:21: error: call to non-static member function without an object argument
func = TestClass::TestMethod;
~~~~~~~~~~~^~~~~~~~~~
编译器尝试在 tc
上调用名为 func
的函数,而不是 func
指向的函数。在我看来,func
的声明方式不正确(作为指向成员函数的指针):
testPointerToMemberFunc.cpp:17:6: error: no member named 'func' in 'TestClass'
tc.func(10, 20);
~~ ^
我做错了什么?
func = TestClass::TestMethod;
应该是func = &TestClass::TestMethod;
,tc.func(10, 20)
应该是(tc.*func)(10, 20)
(后面注意有两个 更改:.
变为 .*
,并且添加了括号;两者都是必需的)。
简单的语法细节。
func = &TestClass::TestMethod;
// ^ Missing ampersand to form a pointer-to-member
TestClass tc;
(tc.*func)(10, 20);
// ^^ Using the dot-star operator to dereference the pointer-to-member,
// while minding its awkward precedence with respect to the call operator.
指向成员(函数或其他)的指针与常规指针有很大不同,尽管在语法上有一些相似之处。常规函数充当函数指针的方式,反之亦然,是从 C 继承的,但 C 不支持指向成员的指针,因此 C++ 中没有必要以这种方式工作。
要创建指向成员的指针,您必须显式使用 &
,并且必须显式使用 .*
来间接指向它,如果您不这样做,这可能更符合您的期望不习惯函数在 C 中的工作方式:
func = &TestClass::TestMethod;
(tc.*func)(10, 20);
我正试图围绕指向成员函数的指针进行思考,并被这个例子所困:
#include <iostream>
class TestClass {
public:
void TestMethod(int, int) const;
};
void TestClass::TestMethod(int x, int y) const {
std::cout << x << " + " << y << " = " << x + y << std::endl;
}
int main() {
void (TestClass::*func)(int, int) const;
func = TestClass::TestMethod;
TestClass tc;
tc.func(10, 20);
return 0;
}
我认为代码应该做什么:
- 在 main 的第一行我声明了一个指向
class TestClass
的成员函数的指针,其中 returns nothing/takes 两个int
并且被声明为const
,称为 func. - 第二行将成员函数
TestMethod
(属于 classTestClass
)分配给满足这些条件的func
。 - 第四行创建一个名为 tc 的
TestClass
对象。 - 第五行尝试在
TestClass
对象tc
. 上调用
func
指向的成员函数
我遇到两个编译错误:
而不是将
TestClass::TestMethod
分配给func
。编译器尝试调用TestClass::TestMethod
(即使它不是static
,因此抛出错误):testPointerToMemberFunc.cpp:14:21: error: call to non-static member function without an object argument func = TestClass::TestMethod; ~~~~~~~~~~~^~~~~~~~~~
编译器尝试在
tc
上调用名为func
的函数,而不是func
指向的函数。在我看来,func
的声明方式不正确(作为指向成员函数的指针):testPointerToMemberFunc.cpp:17:6: error: no member named 'func' in 'TestClass' tc.func(10, 20); ~~ ^
我做错了什么?
func = TestClass::TestMethod;
应该是func = &TestClass::TestMethod;
,tc.func(10, 20)
应该是(tc.*func)(10, 20)
(后面注意有两个 更改:.
变为 .*
,并且添加了括号;两者都是必需的)。
简单的语法细节。
func = &TestClass::TestMethod;
// ^ Missing ampersand to form a pointer-to-member
TestClass tc;
(tc.*func)(10, 20);
// ^^ Using the dot-star operator to dereference the pointer-to-member,
// while minding its awkward precedence with respect to the call operator.
指向成员(函数或其他)的指针与常规指针有很大不同,尽管在语法上有一些相似之处。常规函数充当函数指针的方式,反之亦然,是从 C 继承的,但 C 不支持指向成员的指针,因此 C++ 中没有必要以这种方式工作。
要创建指向成员的指针,您必须显式使用 &
,并且必须显式使用 .*
来间接指向它,如果您不这样做,这可能更符合您的期望不习惯函数在 C 中的工作方式:
func = &TestClass::TestMethod;
(tc.*func)(10, 20);