Hiding Class Names

You can hide class names by declaring a function, object or variable, or enumerator in the same scope. However, the class name can still be accessed when prefixed by the keyword class.

// Declare class Account at file scope.
class Account
{
public:
    Account( double InitialBalance )
        { balance = InitialBalance; }
    double GetBalance()
        { return balance; }
private:
    double balance;
};

double Account = 15.37;            // Hides class name Account

void main()
{
    class Account Checking( Account ); // Qualifies Account as 
                                       //  class name

    cout << "Opening account with balance of: "
         << Checking.GetBalance() << "\n";
}

Note that any place the class name (Account) is called for, the keyword class must be used to differentiate it from the file-scoped variable Account. This rule does not apply when the class name occurs on the left side of the scope-resolution operator (::). Names on the left side of the scope-resolution operator are always considered class names. The following example demonstrates how to declare a pointer to an object of type Account using the class keyword:

class Account *Checking = new class Account( Account );

The Account in the initializer (in parentheses) in the preceding statement has file scope; it is of type double.

Note   The reuse of identifier names as shown in this example is considered poor programming style.

For more information about pointers, see Derived Types. For information about declaration and initialization of class objects, see Chapter 8, Classes. For information about using the new and delete free-store operators, see Chapter 11, Special Member Functions.