Online Book Reader

Home Category

Professional C__ - Marc Gregoire [84]

By Root 1184 0
constructor

A copy constructor With no arguments:

SpreadsheetCell cell;

As a copy of another object:

SpreadsheetCell myCell(cell);

A 0-argument

constructor only A copy constructor With no arguments:

SpreadsheetCell cell;

As a copy of another object:

SpreadsheetCell myCell(cell);

A copy constructor

only No constructors Theoretically, as a copy of another object.

Practically, you can’t create any objects.

A single-argument

(non-copy constructor)

or multi-argument

constructor only A copy constructor With arguments:

SpreadsheetCell cell(6);

As a copy of another object:

SpreadsheetCell myCell(cell);

A 0-argument constructor as well

as a single-argument

constructor (non-

copy constructor)

or multi-argument

constructor A copy constructor With no arguments:

SpreadsheetCell cell;

With arguments:

SpreadsheetCell myCell(5);

As a copy of another object:

SpreadsheetCell anotherCell(cell);

Note the lack of symmetry between the default constructor and the copy constructor. As long as you don’t define a copy constructor explicitly, the compiler creates one for you. On the other hand, as soon as you define any constructor, the compiler stops generating a default constructor.

As mentioned before in this chapter, the automatic generation of a default constructor and a default copy constructor can be influenced in C++11 by defining them as explicitly defaulted or explicitly deleted.

Object Destruction

When an object is destroyed, two events occur: The object’s destructor method is called, and the memory it was taking up is freed. The destructor is your chance to perform any cleanup work for the object, such as freeing dynamically allocated memory or closing file handles. If you don’t declare a destructor, the compiler will write one for you that does recursive memberwise destruction and allows the object to be deleted. The section on dynamic memory allocation in Chapter 7 shows you how to write a destructor.

Objects on the stack are destroyed when they go out of scope, which means whenever the current function, method, or other execution block ends. In other words, whenever the code encounters an ending curly brace, any objects created on the stack within those curly braces are destroyed. The following program shows this behavior:

int main()

{

SpreadsheetCell myCell(5);

if (myCell.getValue() == 5) {

SpreadsheetCell anotherCell(6);

} // anotherCell is destroyed as this block ends.

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

return 0;

} // myCell is destroyed as this block ends.

Code snippet from Destructors\DestructorExamples.cpp

Objects on the stack are destroyed in the reverse order of their declaration (and construction). For example, in the following code fragment, myCell2 is allocated before anotherCell2, so anotherCell2 is destroyed before myCell2 (note that you can start a new code block at any point in your program with an opening curly brace):

{

SpreadsheetCell myCell2(4);

SpreadsheetCell anotherCell2(5); // myCell2 constructed before anotherCell2

} // anotherCell2 destroyed before myCell2

Code snippet from Destructors\DestructorExamples.cpp

This ordering applies to objects that are data members of other objects. Recall that data members are initialized in the order of their declaration in the class. Thus, following the rule that objects are destroyed in the reverse order of their construction, data member objects are destroyed in the reverse order of their declaration in the class.

Objects allocated on the heap are not destroyed automatically. You must call delete on the object pointer to call its destructor and free the memory. The following program shows this behavior:

int main()

{

SpreadsheetCell* cellPtr1 = new SpreadsheetCell(5);

SpreadsheetCell* cellPtr2 = new SpreadsheetCell(6);

cout << "cellPtr1: " << cellPtr1->getValue() << endl;

delete cellPtr1; // Destroys cellPtr1

cellPtr1 = nullptr;

return 0;

} // cellPtr2 is NOT destroyed because delete was not called on it.

Code snippet from Destructors\DestructorHeapExamples.cpp

Do not write programs like the

Return Main Page Previous Page Next Page

®Online Book Reader