Member data cannot be declared as auto, extern, or register storage class. They can, however, be declared as having static storage class.
The decl-specifiers specifiers can be omitted in member-function declarations. (For information on decl-specifiers, see Specifiers in Chapter 6 and Member Functions; see also Functions in Chapter 7.) The following code is therefore legal and declares a function that returns type int:
class NoDeclSpec
{
public:
NoSpecifiers();
};
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 in Chapter 6, and Friends in Chapter 10. 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 HasFriends
{
public:
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 AlreadyDeclared
{
...
};
class HasFriends
{
public:
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 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 in Chapter 9.)