C ++中此星号(*)的含义是什么? -指向成员的指针 [英] What is the meaning of this star (*) symbol in C++? -- Pointer to member
问题描述
在我的项目中,MyClass.h
文件中有几行
In my project, there are a few lines in a MyClass.h
file
class MyClass{
public:
....
#ifndef __MAKECINT__
std::vector<Status_e(MyClass::*)(TupleInfo & info)> m_processObjects; //!
#endif
....
}
//The Status_e is an enum type
//The TupleInfo is a struct
我不明白上面的代码片段中此*
的用法.在MyClass.cxx
文件中,m_processObjects
用作:
I can't understand what the usage of this *
in above code snippet. And in the MyClass.cxx
file, the m_processObjects
used as this:
for (unsigned int f = 0; f < m_processObjects.size(); ++f) {
status = (this->*m_processObjects[f])( m_tuples[i] );
if ( status == FAILURE ) {
return EL::StatusCode::FAILURE;
}
....
}
....
我从未听说过像this->*blabla
这样的用法,但是此代码段有效.那是什么意思?
I have never heard about the usage like this->*blabla
, but this code snippet works. So what it means?
推荐答案
::*
表示与周围的代码一起,它实际上是指向成员函数的指针.
With the surrounding code it's actually a Pointer to member function.
Status_e(MyClass::*)(TupleInfo & info)
是class MyClass
的成员函数,返回Status_e
,并具有一个参数TupleInfo&
. (参数名称info
在这里没有用,但显然被编译器默默忽略了.)
is a member function of class MyClass
, returning Status_e
, and having one paramter TupleInfo&
. (The argument name info
is useless here but obviously silently ignored by the compiler.)
OP问题中的另一个代码片段显示了如何调用它:
The other snippet in OP's question shows how to call it:
status = (this->*m_processObjects[f])(m_tuples[i]);
存储方法指针如下所示:
Storing a method pointer would look like this:
std::vector<Status_e(MyClass::*)(TupleInfo & info)> m_processObjects;
...
m_processObjects.push_back(&MyClass::aMethod);
当然,MyClass::aMethod
的签名必须匹配.
Of course, the signature of MyClass::aMethod
must match.
一个简单的示例来演示它:
A simplified sample to demonstrate it:
#include <iostream>
#include <vector>
class Test {
private:
std::vector<int(Test::*)(const char*)> _tblTestFuncs;
public:
Test()
{
_tblTestFuncs.push_back(&Test::func1);
_tblTestFuncs.push_back(&Test::func2);
_tblTestFuncs.push_back(&Test::func3);
}
int func1(const char *caller) { std::cout << "Test::func1 called from '"<< caller << "': "; return 1; }
int func2(const char *caller) { std::cout << "Test::func2 called from '"<< caller << "': "; return 2; }
int func3(const char *caller) { std::cout << "Test::func3 called from '"<< caller << "': "; return 3; }
void call()
{
for (size_t i = 0, n = _tblTestFuncs.size(); i < n; ++i) {
int result = (this->*_tblTestFuncs[i])("Test::call()");
std::cout << "Return: " << result << '\n';
}
}
};
int main()
{
Test test;
// test method vector in main()
std::vector<int(Test::*)(const char*)> tblTestFuncs;
tblTestFuncs.push_back(&Test::func1);
tblTestFuncs.push_back(&Test::func2);
tblTestFuncs.push_back(&Test::func3);
for (size_t i = 0, n = tblTestFuncs.size(); i < n; ++i) {
int result = (test.*tblTestFuncs[i])("main()");
std::cout << "Return: " << result << '\n';
}
// test method vector in Test
test.call();
// done
return 0;
}
输出:
Test::func1 called from 'main()': Return: 1
Test::func2 called from 'main()': Return: 2
Test::func3 called from 'main()': Return: 3
Test::func1 called from 'Test::call()': Return: 1
Test::func2 called from 'Test::call()': Return: 2
Test::func3 called from 'Test::call()': Return: 3
这篇关于C ++中此星号(*)的含义是什么? -指向成员的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!