参考成员函数?

编程入门 行业动态 更新时间:2024-10-24 11:21:31
本文介绍了参考成员函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我最近发现在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++.

更多推荐

参考成员函数?

本文发布于:2023-07-29 08:46:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239198.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   成员

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!