C ++方法名称是否可以通过头文件中的类名来限定?(Can C++ method names be qualified by the class name, in the header?)

编程入门 行业动态 更新时间:2024-10-25 04:23:22
C ++方法名称是否可以通过头文件中的类名来限定?(Can C++ method names be qualified by the class name, in the header?)

简单的问题是,这个有效的C ++:

class Foo { void Foo::doSomething(); };

问题的关键在于:在类声明中有效的方法名称之前是否重复使用了类名和双冒号?

我遇到了编译代码的问题,它使用g ++ 4.2.3来做到这一点。 在挖掘和更改代码之前,我希望看到对描述此语法的内容的引用。 或降级编译器; 这确实使用g ++ 3.3.6构建。

我得到的错误是(大致):

Foo.h:3: error: extra qualification ‘Foo::’ on member ‘doSomething’

我做了谷歌,但不能拿出一些东西。 我没有这个标准,即使我这样做了,也许会花费我很长一段时间才能找到具有权威性的东西。 我不是C ++语言的律师。

Simple question, is this valid C++:

class Foo { void Foo::doSomething(); };

The point of the question: is that repeated use of the class name and double colon before the method name valid inside the class declaration?

I'm having issues compiling code that does this using g++ 4.2.3. I would love to see a reference to something describing the syntax here, before digging in and changing the code. Or downgrading the compiler; this does build with g++ 3.3.6.

The error I'm getting is (roughly):

Foo.h:3: error: extra qualification ‘Foo::’ on member ‘doSomething’

I did Google, but couldn't come up with something. I don't have the standard, and even if I did it would probably take me quite a while to find anything authoritative. I'm no C++ language lawyer.

最满意答案

我看了一下标准,第9.2节将是相关部分。 我对BNF并不是很了不起,但是我没有看到BNF上的任何类别成员都会表示这是允许的。 该标识符在BNF中甚至被命名为“非限定标识”。

G ++在4.1版中改变了行为,显然很多其他编译器都接受了这一点,但我从未见过这种风格,我不知道为什么有人会这样做。 由于它似乎不会在各种各样的编译器上触发错误,所以这种风格可能有一些历史原因,但据我所知它确实是无效的。

我通过Google发现的唯一的好参考就是这个页面 ,它只是试图解释一些G ++ 4.1的变化。

I took a look at the standard, section 9.2 would be the relevant portion. I'm not that great with BNF but I didn't see anything in the BNF for class members that would indicate this was allowed. The identifier is even named "unqualified-id" in the BNF.

G++ changed the behavior in version 4.1, and apparently a lot of other compilers accepted this, but I've never seen this style used and I have no idea why anyone would do it. Since it seems to not trigger an error on a pretty wide variety of compilers, there may be some historical reason for this style, but as far as I can tell it's indeed not valid.

The only good reference I found through Google was this page, which just attempts to explain some of the changes in G++ 4.1.

更多推荐

本文发布于:2023-08-05 03:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1427547.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可以通过   头文件   名称   方法   类名来

发布评论

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

>www.elefans.com

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