Online Book Reader

Home Category

Professional C__ - Marc Gregoire [98]

By Root 1152 0

const SpreadsheetCell& rhs)

{

if (this == &rhs) {

return *this;

}

mValue = rhs.mValue;

mString = rhs.mString;

mNumAccesses = rhs.mNumAccesses;

return *this;

}

Code snippet from NestedClasses\Spreadsheet.cpp

In fact, you must even use the syntax for return types (but not parameters) of methods in the Spreadsheet class itself:

Spreadsheet::SpreadsheetCell Spreadsheet::getCellAt(int x, int y)

{

if (!inRange(x, mWidth) || !inRange(y, mHeight)) {

throw std::out_of_range("");

}

return mCells[x][y];

}

Code snippet from NestedClasses\Spreadsheet.cpp

You can avoid the clumsy syntax by using a typedef to rename Spreadsheet::SpreadsheetCell to something more manageable like SCell:

typedef Spreadsheet::SpreadsheetCell SCell;

Code snippet from NestedClasses\Spreadsheet.h

This typedef should go outside the Spreadsheet class definition, or else you will have to qualify the typedef name itself with Spreadsheet:: to get Spreadsheet::SCell. That wouldn’t do you much good!

Now you can write your constructor like this:

SCell::SpreadsheetCell() : mValue(0), mNumAccesses(0)

{

}

Code snippet from NestedClasses\Spreadsheet.cpp

Normal access control applies to nested class definitions. If you declare a private or protected nested class, you can only use it inside the outer class.

You should generally use nested class definitions only for trivial classes. It is really too clumsy for something like the SpreadsheetCell class.

ENUMERATED TYPES INSIDE CLASSES


If you want to define a number of constants inside a class, you should use an enumerated type instead of a collection of #defines. For example, you can add support for cell coloring to the SpreadsheetCell class as follows:

class SpreadsheetCell

{

public:

// Omitted for brevity

typedef enum {Red=1, Green, Blue, Yellow} Colors;

void setColor(Colors color);

protected:

// Omitted for brevity

Colors mColor = Red;

};

Code snippet from SpreadsheetCellColors\SpreadsheetCell.h

The implementation of the setColor() method is straightforward:

void SpreadsheetCell::setColor(Colors color)

{

mColor = color;

}

Code snippet from SpreadsheetCellColors\SpreadsheetCell.cpp

The new method can be used as follows:

SpreadsheetCell myCell(5);

myCell.setColor(SpreadsheetCell::Blue);

Code snippet from SpreadsheetCellColors\SpreadsheetCellTest.cpp

Using an enumerated type is the preferred solution instead of using #defines as follows:

#define SPREADSHEETCELL_RED 1

#define SPREADSHEETCELL_GREEN 2

#define SPREADSHEETCELL_BLUE 3

#define SPREADSHEETCELL_YELLOW 4

class SpreadsheetCell

{

public:

// Omitted for brevity

void setColor(int color);

protected:

// Omitted for brevity

int mColor;

};

When you use #defines, you have to use an integer parameter for the setColor() function instead of a clear type like the Colors enumerated type.

FRIENDS


C++ allows classes to declare that other classes or member functions of other classes or nonmember functions are friends, and can access protected and private data members and methods. For example, the SpreadsheetCell class could specify that the Spreadsheet class is its “friend” like this:

class SpreadsheetCell

{

public:

friend class Spreadsheet;

// Remainder of the class omitted for brevity

};

Now all the methods of the Spreadsheet class can access the private and protected data members and methods of the SpreadsheetCell class.

If you only want to make a specific member function of the Spreadsheet class a friend, you can do that as follows:

class SpreadsheetCell

{

public:

friend void Spreadsheet::setCellAt(int x, int y,

const SpreadsheetCell& cell);

// Remainder of the class omitted for brevity

};

Code snippet from Friends\FriendMethod\SpreadsheetCell.h

Note that a class needs to know which other classes, methods, or functions wish to be its friends; a class, method or function cannot declare itself to be a friend of some other class and access the non-public names of that class.

You might, for example, want to write a function to verify that the string of a SpreadsheetCell object

Return Main Page Previous Page Next Page

®Online Book Reader