Online Book Reader

Home Category

Professional C__ - Marc Gregoire [81]

By Root 1418 0
SpreadsheetCellDefaultCtor\SpreadsheetCellInitListBackward.cpp

The code will compile (although some compilers issue a warning), but the program does not work correctly. You might assume that mString will be initialized before mValue because mString is listed first in the ctor-initializer. But C++ doesn’t work that way. The SpreadsheetCell class declares mValue before mString; thus, the ctor-initializer tries to initialize mValue before mString. However, the code to initialize mValue tries to use the value of mString, which is not yet initialized! The solution in this case is to use the initialValue argument instead of mString when initializing mValue. You should also swap their order in the ctor-initializer to avoid confusion:

SpreadsheetCell::SpreadsheetCell(string initialValue) :

mValue(stringToDouble(initialValue)), mString(initialValue)

{

}

Code snippet from SpreadsheetCellDefaultCtor\SpreadsheetCellInitList.cpp

Ctor-Initializers initialize data members in their declared order in the class definition, not their order in the ctor-initializer list.

Copy Constructors

There is a special constructor in C++ called a copy constructor that allows you to create an object that is an exact copy of another object. If you don’t write a copy constructor yourself, C++ generates one for you that initializes each data member in the new object from its equivalent data member in the source object. For object data members, this initialization means that their copy constructors are called.

Here is the declaration for a copy constructor in the SpreadsheetCell class:

class SpreadsheetCell

{

public:

SpreadsheetCell(const SpreadsheetCell& src);

// Remainder of the class definition omitted for brevity

};

Code snippet from SpreadsheetCellCopyCtor\SpreadsheetCell.h

The copy constructor takes a const reference to the source object. Like other constructors, it does not return a value. Inside the constructor, you should copy all the data fields from the source object. Technically, of course, you can do whatever you want in the copy constructor, but it’s generally a good idea to follow expected behavior and initialize the new object to be a copy of the old one. Here is a sample implementation of the SpreadsheetCell copy constructor:

SpreadsheetCell::SpreadsheetCell(const SpreadsheetCell& src) :

mValue(src.mValue), mString(src.mString)

{

}

Code snippet from SpreadsheetCellCopyCtor\SpreadsheetCell.cpp

Note the use of the ctor-initializer. The difference between setting values in the ctor-initializer and in the copy constructor body is examined later in the section on assignment.

Given a set of member variables, called m1, m2, ... mn, the compiler-generated copy constructor can be expressed as:

classname::classname(const classname& src) :

m1(src.m1), m2(src.m2), ... mn(src.mn) { }

Therefore, in most circumstances, there is no need for you to specify a copy constructor yourself. However, under certain conditions, this default copy constructor is not sufficient. These conditions are covered in Chapter 7.

When the Copy Constructor Is Called

The default semantics for passing arguments to functions in C++ is pass-by-value. That means that the function or method receives a copy of the value or object. Thus, whenever you pass an object to a function or method the compiler calls the copy constructor of the new object to initialize it. For example, recall that the definition of the setString() method in the SpreadsheetCell class looks like this:

void SpreadsheetCell::setString(string inString)

{

mString = inString;

mValue = stringToDouble(mString);

}

Recall, also, that the C++ string is actually a class, not a built-in type. When your code makes a call to setString() passing a string argument, the string parameter inString is initialized with a call to its copy constructor. The argument to the copy constructor is the string you passed to setString(). In the following example, the string copy constructor is executed for the inString object in setString() with name as its parameter.

SpreadsheetCell myCell;

Return Main Page Previous Page Next Page

®Online Book Reader