Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference to member function? [duplicate]

I recently find out that there is a reference-to-function concept in C++ :). So as there are pointer-to-function and pointer-to-member-function different types. The question arises. Is there a "reference-to-member-function" concept?

I tried to compile the following code, but GCC 3.4.6 gives an error.

#include <iostream>

using namespace std;

class A {
public:
  virtual void Af() const {
    cout << "A::Af()" << endl;
  }
};

int main() {
  typedef void (A::& MemFnc)() const;
  MemFnc mf = &A::Af;

  A a;
  (a.*mf)();

  return 0;
}
like image 997
MKo Avatar asked Aug 30 '11 15:08

MKo


People also ask

How to copy References c++?

C++ gives you the choice: use the assignment operator to copy the value (copy/value semantics), or use a pointer-copy to copy a pointer (reference semantics). C++ allows you to override the assignment operator to do anything your heart desires, however the default (and most common) choice is to copy the value.

What is constant member function in c++?

The const member functions are the functions which are declared as constant in the program. The object called by these functions cannot be modified. It is recommended to use const keyword so that accidental changes to object are avoided. A const member function can be called by any type of object.

What is the syntax of a const member function?

To make a member function constant, the keyword “const” is appended to the function prototype and also to the function definition header. Like member functions and member function arguments, the objects of a class can also be declared as const.

What is ref qualifier C++?

Ref-qualifiers are used to choose between normal and rvalue reference semantics, allowing the compiler to use either copy or move semantics depending on which are more appropriate, and are applied to *this instead of this .


3 Answers

There is no such a thing called reference to member in C++.

The language specification explicitly says in a note (§8.3.3/3 - 2003) that,

A pointer to member shall not point to a static member of a class (9.4), a member with reference type, or “cv void.” [Note: see also 5.3 and 5.5. The type “pointer to member” is distinct from the type “pointer”, that is, a pointer to member is declared only by the pointer to member declarator syntax, and never by the pointer declarator syntax. There is no “reference-to-member” type in C++.

like image 158
Nawaz Avatar answered Oct 25 '22 12:10

Nawaz


No, references to member functions are not possible.

In some sense, the result of dereferencing a pointer to a member function could serve as one, but the only thing you can do with that result is to invoke a function call operator on it, per 5.5[expr.mptr.oper]/6. Nothing else is allowed.

like image 41
Cubbi Avatar answered Oct 25 '22 11:10

Cubbi


There is no reference to member function.

like image 29
Tom Kerr Avatar answered Oct 25 '22 12:10

Tom Kerr