gpt4 book ai didi

c++ - 成员函数声明中是否允许限定名称?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:05:48 26 4
gpt4 key购买 nike

这段代码被MSVC9.0接受。我的问题是根据标准(旧的和/或新的)它是否合法。也非常欢迎引用。

class X
{
void X::f();
};

最佳答案

不,这是无效的。这里,X::f 是一个限定名;您正试图将其用作 declarator-id。 C++03 8.3[dcl.meaning]/1 列出了declarator-id 可能被限定的情况:

A declarator-id shall not be qualified except for

  • the definition of a member function or static data member outside of its class,

  • the definition or explicit instantiation of a function or variable member of a namespace outside of its namespace, or

  • the definition of a previously declared explicit specialization outside of its namespace, or

  • the declaration of a friend function that is a member of another class or namespace.

因为 X::f 不属于这四个类别中的任何一个,所以它是不正确的。

在C++03 9.3[class.mfct]/5中可以找到要求在类定义之外定义成员函数的规则:

If the definition of a member function is lexically outside its class definition, the member function name shall be qualified by its class name using the :: operator.

关于c++ - 成员函数声明中是否允许限定名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7111957/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com