Access Specifiers

In class declarations, members can have access specifiers.

Syntax

access-specifier  :  member-listopt

The access-specifier determines the access to the names that follow it, up to the next access-specifier or the end of the class declaration. Figure 10.1 illustrates this concept.

Figure 10.1   Access Control in Classes

Although only two access specifiers are shown in Figure 10.1, there is no limit to the number of access specifiers in a given class declaration. For example, the Point class in Figure 10.1 could just as easily be declared using multiple access specifiers as follows:

class Point
{
public:                  // Declare public constructor.
    Point( int, int );
private:                 // Declare private state variable.
    int _x;
public:                  // Declare public constructor.
    Point();
public:                  // Declare public accessor.
    int &x( int );
private:                 // Declare private state variable.
    int _y;
public:                  // Declare public accessor.
    int &y( int );
};

Note that there is no specific order required for member access, as shown in the preceding example. The allocation of storage for objects of class types is implementation dependent, but members are guaranteed to be assigned successively higher memory addresses between access specifiers.