简体   繁体   English

具有类名的数据成员

[英]data member with the class name

The standard says, "A member of a class T cannot use T as its name if the member is a static data member, a member function, a member type, a member template, an enumerator of an unscoped enumeration, a member of a member anonymous union. However, a non-static data member may use the name T as long as there are no user-declared constructors." 该标准说:“如果类T的成员是静态数据成员,成员函数,成员类型,成员模板,无范围枚举的枚举器,成员的成员,则T类的成员不能使用T作为其名称。匿名联合。但是,只要没有用户声明的构造函数,一个非静态数据成员就可以使用名称T。”

However if I create this class, it gives an compile error. 但是,如果我创建此类,它将给出编译错误。

class G
{
    int G;
};

I am using VS2013. 我正在使用VS2013。 Is it not allowed in Microsoft or ? Microsoft不允许吗?

If VC++ doesn't allow this, it's a bug. 如果VC ++不允许这样做,那就是一个错误。

However, this language "feature" is for the purpose of C compatibility, and Microsoft has decided not to emphasize C. For example, C99 features are unavailable as a rule until adopted by C++. 但是,此语言“功能”是为了实现C兼容性,并且Microsoft决定不强调C。例如,在C ++采纳之前,通常不提供C99功能。 You should never purposely declare such a member in C++. 您绝对不应在C ++中故意声明这样的成员。

(It's allowed in C simply by default: there are no restrictions on the naming of members, and all members are nonstatic data members.) (默认情况下,仅在C语言中允许这样做:成员的命名没有限制,并且所有成员都是非静态数据成员。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM