Online Book Reader

Home Category

Professional C__ - Marc Gregoire [93]

By Root 1323 0
from a constructor. So, you need to be careful with this. Details are explained in Chapter 10.

kMaxHeight and kMaxWidth are public, so you can access them from anywhere in your program as if they were global variables, but with slightly different syntax: You must specify that the variable is part of the Spreadsheet class with the :: scope resolution operator:

cout << "Maximum height is: " << Spreadsheet::kMaxHeight << endl;

Reference Data Members

Spreadsheets and SpreadsheetCells are great, but they don’t make a very useful application by themselves. You need code to control the whole spreadsheet program, which you could package into a SpreadsheetApplication class.

The implementation of this class is unimportant at the moment. For now, consider this architecture problem: How can spreadsheets communicate with the application? The application stores a list of spreadsheets, so it can communicate with the spreadsheets. Similarly, each spreadsheet should store a reference to the application object. The Spreadsheet class must know about the SpreadsheetApplication class and the SpreadsheetApplication class must know about the Spreadsheet class. This is a circular reference and cannot be solved with normal #includes. The solution is to use a forward declaration in one of the header files (see Chapter 9 for details). Here is the new Spreadsheet class definition that uses a forward declaration to tell the compiler about the SpreadsheetApplication class:

class SpreadsheetApplication; // forward declaration

class Spreadsheet

{

public:

Spreadsheet(int inWidth, int inHeight,

SpreadsheetApplication& theApp);

// Code omitted for brevity.

protected:

// Code omitted for brevity.

SpreadsheetApplication& mTheApp;

};

Code snippet from SpreadsheetDataMembers\Spreadsheet.h

This definition adds a SpreadsheetApplication reference as a data member. It’s recommended to use a reference in this case instead of a pointer because a Spreadsheet should always refer to a SpreadsheetApplication. This would not be guaranteed with a pointer.

Note that the application reference is given to each Spreadsheet in its constructor. A reference cannot exist without referring to something, so mTheApp must be given a value in the ctor-initializer of the constructor:

Spreadsheet::Spreadsheet(int inWidth, int inHeight,

SpreadsheetApplication& theApp)

: mWidth(inWidth < kMaxWidth ? inWidth : kMaxWidth),

mHeight(inHeight < kMaxHeight ? inHeight : kMaxHeight), mTheApp(theApp)

{

// Code omitted for brevity.

}

Code snippet from SpreadsheetDataMembers\Spreadsheet.cpp

You must also initialize the reference member in the copy constructor:

Spreadsheet::Spreadsheet(const Spreadsheet& src) :

mTheApp(src.mTheApp)

{

mId = sCounter++;

copyFrom(src);

}

Code snippet from SpreadsheetDataMembers\Spreadsheet.cpp

Remember that after you have initialized a reference you cannot change the object to which it refers. Thus, you do not need to attempt to assign to references in the assignment operator.

const Reference Data Members

Your reference members can refer to const objects just as normal references can refer to const objects. For example, you might decide that Spreadsheets should only have a const reference to the application object. You can simply change the class definition to declare mTheApp as a const reference:

class Spreadsheet

{

public:

Spreadsheet(int inWidth, int inHeight,

const SpreadsheetApplication& theApp);

// Code omitted for brevity.

protected:

// Code omitted for brevity.

const SpreadsheetApplication& mTheApp;

};

Code snippet from SpreadsheetDataMembers\Spreadsheet.h

There is an important difference between using a const reference versus a non-const reference. The const reference SpreadsheetApplication data member can only be used to call const methods on the SpreadsheetApplication object. If you try to call a non-const method through a const reference, you will get a compiler error.

It’s also possible to have a static reference member or a static const reference member, but you will rarely find the need for something

Return Main Page Previous Page Next Page

®Online Book Reader