code_not_yet_complete
code_not_yet_complete

Reputation: 143

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 ?

Upvotes: 8

Views: 399

Answers (1)

Potatoswatter
Potatoswatter

Reputation: 137850

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

Upvotes: 3

Related Questions