Online Book Reader

Home Category

Professional C__ - Marc Gregoire [78]

By Root 1291 0

}

That seems to make sense. After all, you can call normal class methods from within other methods. The code will compile, link, and run, but will not do what you expect. The explicit call to the SpreadsheetCell constructor actually creates a new temporary unnamed object of type SpreadsheetCell. It does not call the constructor for the object that you are supposed to be initializing.

C++11 introduces a new feature called delegating constructors, which allows constructors to call another constructor from the same class. This will be discussed later in this chapter.

Default Constructors

A default constructor is a constructor that takes no arguments. It is also called a 0-argument constructor. With a default constructor, you can give initial values to data members even though the client did not specify them.

When You Need a Default Constructor

Consider arrays of objects. The act of creating an array of objects accomplishes two tasks: It allocates contiguous memory space for all the objects and it calls the default constructor on each object. C++ fails to provide any syntax to tell the array creation code directly to call a different constructor. For example, if you do not define a default constructor for the SpreadsheetCell class, the following code does not compile:

SpreadsheetCell cells[3]; // FAILS compilation without default constructor

SpreadsheetCell* myCellp = new SpreadsheetCell[10]; // Also FAILS

You can circumvent this restriction for stack-based arrays by using initializers like these:

SpreadsheetCell cells[3] = {SpreadsheetCell(0), SpreadsheetCell(23),

SpreadsheetCell(41)};

However, it is usually easier to ensure that your class has a default constructor if you intend to create arrays of objects of that class.

A default constructor is also required for classes that you want to store in an STL container like std::vector. STL containers are explained in detail in Chapter 12.

Default constructors are also useful when you want to create objects of that class inside other classes, which is shown later in this chapter under the section Constructor Initializers.

Finally, default constructors are convenient when the class serves as a base class of an inheritance hierarchy. In that case, it’s convenient for subclasses to initialize superclasses via their default constructors. Chapter 8 covers this issue in more detail.

How To Write a Default Constructor

Here is part of the SpreadsheetCell class definition with a default constructor:

class SpreadsheetCell

{

public:

SpreadsheetCell();

// Remainder of the class definition omitted for brevity

};

Code snippet from SpreadsheetCellDefaultCtor\SpreadsheetCell.h

Here is a first crack at an implementation of the default constructor:

SpreadsheetCell::SpreadsheetCell()

{

mValue = 0;

mString = "";

}

Code snippet from SpreadsheetCellDefaultCtor\SpreadsheetCell.cpp

You use the default constructor on the stack like this:

SpreadsheetCell myCell;

myCell.setValue(6);

cout << "cell 1: " << myCell.getValue() << endl;

Code snippet from SpreadsheetCellDefaultCtor\SpreadsheetCellTest.cpp

The preceding code creates a new SpreadsheetCell called myCell, sets its value, and prints out its value. Unlike other constructors for stack-based objects, you do not call the default constructor with function-call syntax. Based on the syntax for other constructors, you might be tempted to call the default constructor like this:

SpreadsheetCell myCell(); // WRONG, but will compile.

myCell.setValue(6); // However, this line will not compile.

cout << "cell 1: " << myCell.getValue() << endl;

Unfortunately, the line attempting to call the default constructor will compile. The line following it will not compile. The problem is that your compiler thinks the first line is actually a function declaration for a function with the name myCell that takes zero arguments and returns a SpreadsheetCell object. When it gets to the second line, it thinks that you’re trying to use a function name as an object!

When creating an object on the stack, omit parentheses for the default

Return Main Page Previous Page Next Page

®Online Book Reader