Online Book Reader

Home Category

Professional C__ - Marc Gregoire [79]

By Root 1116 0
constructor.

However, when you use the default constructor with a heap-based object allocation, you are required to use function-call syntax:

SpreadsheetCell* myCellp = new SpreadsheetCell(); // Note the function-call syntax

Code snippet from SpreadsheetCellDefaultCtor\SpreadsheetCellTest.cpp

Don’t waste a lot of time pondering why C++ requires different syntax for heap-based versus stack-based object allocation with a default constructor. It’s just one of those things that makes C++ such an exciting language to learn.

Compiler-Generated Default Constructor

The first SpreadsheetCell class definition in this chapter looked as follows:

class SpreadsheetCell

{

public:

void setValue(double inValue);

double getValue() const;

protected:

double mValue;

};

Code snippet from SpreadsheetCellNumOnly\SpreadsheetCell.h

This definition does not declare a default constructor, but still, the code that follows works perfectly.

SpreadsheetCell myCell;

myCell.setValue(6);

The following definition is the same as the preceding definition except that it adds an explicit constructor, accepting a double. It still does not explicitly declare a default constructor.

class SpreadsheetCell

{

public:

SpreadsheetCell(double initialValue); // No default constructor

// Remainder of the class definition omitted for brevity

};

Code snippet from SpreadsheetCellCtors\SpreadsheetCell.h

With this definition, the following code will not compile anymore:

SpreadsheetCell myCell;

myCell.setValue(6);

What’s going on here? The reason is that if you don’t specify any constructors, the compiler will write one for you that doesn’t take any arguments. This compiler-generated default constructor calls the default constructor on all object members of the class, but does not initialize the language primitives such as int and double. Nonetheless, it allows you to create objects of that class. However, if you declare a default constructor, or any other constructor, the compiler no longer generates a default constructor for you.

A default constructor is the same thing as a 0-argument constructor. The term default constructor does not refer only to the constructor automatically generated if you fail to declare any constructors. It refers to the constructor which is defaulted to if there are no arguments.

Explicitly Defaulted Constructors

In older versions of C++, if your class required a number of explicit constructors accepting arguments but also a default constructor that does nothing, you had to explicitly write your empty default constructor as follows:

class MyClass

{

public:

MyClass() {}

MyClass(int i);

};

However, it’s recommended that interface files contain only declarations of public methods without any implementations. The preceding class definition violates this. The solution was to define the class as follows:

class MyClass

{

public:

MyClass();

MyClass(int i);

};

The implementation of the empty default constructor in the implementation file would be:

MyClass::MyClass() { }

To avoid having to write empty default constructors manually, C++11 introduces the concept of explicitly defaulted constructors. This allows you to write the class definition as follows without the need to implement it in the implementation file.

class MyClass

{

public:

MyClass() = default;

MyClass(int i);

};

MyClass defines a custom constructor that accepts one integer. However, the compiler will still generate a standard compiler generated default constructor due to the use of the default keyword.

Explicitly Deleted Constructors

C++11 also supports the concept of explicitly deleted constructors. For example, you can define a class for which you do not want to write any constructors and you also do not want the compiler to generate the default constructor. In that case you need to explicitly delete the default constructor:

class MyClass

{

public:

MyClass() = delete;

};

Constructor Initializers

Up to now, this chapter initialized data members in the body of a constructor, for example:

SpreadsheetCell::SpreadsheetCell()

Return Main Page Previous Page Next Page

®Online Book Reader