Condividi tramite


Class-Member Declaration Syntax

Class data members and function members are generally declared following the rules for declarations. They consist of, in this order:

  • Declaration specifiers. In classes, the storage class specifiers static and mutable are allowed for data members. The cv-qualifiers const and volatile are allowed for data and function members. The declaration specifiers friend, static, virtual and inline are allowed for functions, although friend indicates a non-member function.

  • Declarators without initializers. In the case of virtual functions, the pure specifier of the form = 0 is used after the function declarator to specify a pure virtual function (a virtual function without an implementation in that class).

  • Optionally, bit fields. See Bit Fields.

OR

  • Function definitions. The definition of a function can be included in the class. If a member function is so defined, it is automatically considered inline.

OR

Member data cannot be declared as auto, extern, or register storage class. They can, however, be declared as having static storage class.

When you declare a friend class in a member list, you can omit the member declarator list. For more information on friends, see friend Specifier and Friends. Even if a class name has not been introduced, it can be used in a friend declaration. This friend declaration introduces the name. However, in member declarations for such classes, the elaborated type specifier syntax must be used, as shown in the following example:

// class_member_declaration_syntax.cpp
// compile with: /c
struct HasFriends {
   friend class NotDeclaredYet;
};

In the preceding example, there is no member declarator list after the class declaration. Because the declaration for NotDeclaredYet has not yet been processed, the elaborated type specifier form is used: class NotDeclaredYet. A type that has been declared can be specified in a friend member declaration using a normal type specifier:

// class_member_declaration_syntax2.cpp
// compile with: /c
struct AlreadyDeclared {};

struct HasFriends {
   friend AlreadyDeclared;
};

The pure specifier (shown in the following example) indicates that no implementation is supplied for the virtual function being declared. Therefore, the pure specifier can be specified only on virtual functions. Consider this example:

// class_member_declaration_syntax3.cpp
// compile with: /c
class StrBase {   // Base class for strings.
public:
   virtual int IsLessThan( StrBase& ) = 0;
   virtual int IsEqualTo( StrBase& ) = 0;
   virtual StrBase& CopyOf( StrBase& ) = 0;
};

The preceding code declares an abstract base class — that is, a class designed to be used only as the base class for more specific classes. Such base classes can enforce a particular protocol, or set of functionality, by declaring one or more virtual functions as "pure" virtual functions, using the pure specifier.

Classes that inherit from the StrBase class must provide implementations for the pure virtual functions; otherwise, they, too, are considered abstract base classes.

Abstract base classes cannot be used to declare objects. For example, before an object of a type inherited from StrBase can be declared, the functions IsLessThan, IsEqualTo, and CopyOf must be implemented. (For more information about abstract base classes, see Abstract Classes.)

See Also

Reference

Class Members