Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 609
code_not_yet_complete Avatar asked Aug 08 '15 20:08

code_not_yet_complete


People also ask

What are the data members of the class?

Data members include members that are declared with any of the fundamental types, as well as other types, including pointer, reference, array types, bit fields, and user-defined types.

How do you find the data members of a class?

Accessing data members and member functions: The data members and member functions of class can be accessed using the dot('.') operator with the object.

What is the name of the data member?

The variables which are declared in any class by using any fundamental data types (like int, char, float etc) or derived data type (like class, structure, pointer etc.) are known as Data Members. And the functions which are declared either in private section of public section are known as Member functions.

What are data members of a class in Java?

A data member may be of any type, including classes already defined, pointers to objects of any type, or even references to objects of any type. Data members may be private or public, but are usually held private so that values may only be changed at the discretion of the class function members.


1 Answers

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

like image 120
Potatoswatter Avatar answered Oct 06 '22 01:10

Potatoswatter