参考成员函数? [英] Reference to member function?

查看:93
本文介绍了参考成员函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发现在C ++中有一个引用到函数的概念:)。
所以有指针到函数和指针到成员函数不同的类型。问题出现了。是否有引用到成员函数的概念?



我试图编译以下代码,但GCC 3.4.6给出错误。

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


解决方案

((§8.3.3)中的 3 - 2003),


指向成员的指针不能指向类的静态成员(9.4)类型或cv void。[注:另见5.3和5.5。类型指向成员的指针与类型指针不同,即,指向成员的指针仅由指向成员声明符语法的指针声明,并且从不由指针声明符语法声明。 在C ++中没有引用到成员类型。



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

解决方案

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++.

这篇关于参考成员函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆