Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of this star (*) symbol in C++? -- Pointer to member

Tags:

c++

pointers

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

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;
    }
....
}
....

I have never heard about the usage like this->*blabla, but this code snippet works. So what it means?

like image 344
spring cc Avatar asked May 17 '18 06:05

spring cc


People also ask

What does * In pointer mean?

A pointer variable points to a data type (like int ) of the same type, and is created with the * operator.

What does Star mean in pointer?

The * in expression means "the value stored in the address" so if c is a pointer to some char, then *c is the specific char. char *(*s); meaning that s is a pointer to a pointer to char, so s doesn't hold the address of a char, but the address of variable that hold the address of a char.

What does a star mean in C?

In computer programming, the dereference operator or indirection operator, sometimes denoted by " * " (i.e. an asterisk), is a unary operator (i.e. one with a single operand) found in C-like languages that include pointer variables.

What does * After a variable mean in C?

* is the indirection operator in C and C++. Whenever it is used, it indicates that the variable next to it is a pointer containing the address of another variable. Indirection operator is also the "value stored at address" operator.


1 Answers

::* denotes a Pointer to member.

With the surrounding code it's actually a Pointer to member function.

Status_e(MyClass::*)(TupleInfo & info)

is a member function of class MyClass, returning Status_e, and having one parameter TupleInfo&. (The argument name info is useless here but obviously silently ignored by the compiler.)

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);

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;
}

Output:

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

Live Demo on coliru.com

like image 175
Scheff's Cat Avatar answered Oct 04 '22 11:10

Scheff's Cat