Class member lists (C++ only)

An optional member list declares subobjects called class members. Class members can be data, functions, nested types, and enumerators.

Read syntax diagramSkip visual syntax diagram
Class member list syntax

   .-------------------------------------------------------.   
   V                                                       |   
>>---+-member_declaration--+------------------------+--;-+-+---><
     |                     +-=--0-------------------+    |     
     |                     '-=--constant_expression-'    |     
     +-member_definition---------------------------------+     
     '-access_specifier--:-------------------------------'     

The member list follows the class name and is placed between braces. The following applies to member lists, and members of member lists:
  • A member_declaration or a member_definition may be a declaration or definition of a data member, member function, nested type, or enumeration. (The enumerators of a enumeration defined in a class member list are also members of the class.)
  • A member list is the only place where you can declare class members.
  • Friend declarations are not class members but must appear in member lists.
  • The member list in a class definition declares all the members of a class; you cannot add members elsewhere.
  • You cannot declare a member twice in a member list.
  • You may declare a data member or member function as static but not auto, extern, or register.
  • You may declare a nested class, a member class template, or a member function, and define it outside the class.
  • You must define static data members outside the class.
  • Nonstatic members that are class objects must be objects of previously defined classes; a class A cannot contain an object of class A, but it can contain a pointer or reference to an object of class A.
  • You must specify all dimensions of a nonstatic array member.

A constant initializer (= constant_expression) may only appear in a class member of integral or enumeration type that has been declared static.

A pure specifier (= 0) indicates that a function has no definition. It is only used with member functions declared as virtual and replaces the function definition of a member function in the member list.

An access specifier is one of public, private, or protected.

A member declaration declares a class member for the class containing the declaration.

The order of allocation of nonstatic class members separated by an access_specifier is implementation-dependent.

The order of allocation of nonstatic class members separated by an access_specifier is implementation-dependent. The compiler allocates class members in the order in which they are declared.

Suppose A is a name of a class. The following class members of A must have a name different from A:
  • All data members
  • All type members
  • All enumerators of enumerated type members
  • All members of all anonymous union members