简体   繁体   中英

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

However if I create this class, it gives an compile error.

class G
{
    int G;
};

I am using VS2013. Is it not allowed in Microsoft or ?

If VC++ doesn't allow this, it's a bug.

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++. You should never purposely declare such a member in 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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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