Member Functions in Nested Classes

Member functions declared in nested classes can be defined in file scope. The preceding example could have been written:

// member_functions_in_nested_classes.cpp
class BufferedIO
{
public:
    enum IOError { None, Access, General };
    class BufferedInput
    {
    public:
        int read(); // Declare but do not define member
        int good(); //  functions read and good.
    private:
        IOError _inputerror;
    };

    class BufferedOutput
    {
        // Member list.
    };
};
// Define member functions read and good in
//  file scope.
int BufferedIO::BufferedInput::read()
{
   return(1);
}

int BufferedIO::BufferedInput::good()
{
    return _inputerror == None;
}
int main()
{
}

In the preceding example, the qualified-type-name syntax is used to declare the function name. The declaration:

BufferedIO::BufferedInput::read()

means "the read function that is a member of the BufferedInput class that is in the scope of the BufferedIO class." Because this declaration uses the qualified-type-name syntax, constructs of the following form are possible:

typedef BufferedIO::BufferedInput BIO_INPUT;

int BIO_INPUT::read()

The preceding declaration is equivalent to the previous one, but it uses a typedef name in place of the class names.

See Also

Reference

Nested Class Declarations