Online Book Reader

Home Category

Professional C__ - Marc Gregoire [76]

By Root 1103 0

myCellp = nullptr;

Code snippet from SpreadsheetCellNumText\SpreadsheetCellHeapAlternate.cpp

Just as you must free other memory that you allocate on the heap, you must free the memory for objects that you allocate on the heap by calling delete on the objects. To avoid memory problems, it’s highly recommended to use smart pointers as follows:

shared_ptr myCellp(new SpreadsheetCell());

myCellp->setValue(3.7);

cout << "cell 1: " << myCellp->getValue() <<

" " << myCellp->getString() << endl;

Code snippet from SpreadsheetCellNumText\SpreadsheetCellHeapSmartPointer.cpp

With smart pointers you don’t need to manually free the memory, it will happen automatically. Smart pointers are discussed in detail in Chapter 21.

If you allocate an object with new, free it with delete when you are finished with it, or use smart pointers to manage the memory automatically.

If you don’t use smart pointers, it is always a good idea to reset a pointer to the null pointer after deleting the object to which it pointed. You are not required to do this, but it will make debugging easier in case the pointer is accidently used after deleting the object. In the above example, the pointer is reset to nullptr, which is a C++11 specific feature. If your compiler does not yet support nullptr, use “myCellp = NULL” instead.

OBJECT LIFE CYCLES


The object life cycle involves three activities: creation, destruction, and assignment. It is important to understand how and when objects are created, destroyed, and assigned, and how you can customize these behaviors.

Object Creation

Objects are created at the point you declare them (if they’re on the stack) or when you explicitly allocate space for them with new or new[]. When an object is created, all its embedded objects are also created. For example:

#include

class MyClass

{

protected:

std::string mName;

};

int main()

{

MyClass obj;

return 0;

}

The embedded string object is created at the point where the MyClass object is created in the main() function and is destructed when its containing object is destructed.

It is often helpful to give variables initial values as you declare them. For example, you should usually initialize integer variables to 0 like this:

int x = 0;

Similarly, you should give initial values to objects. You can provide this functionality by declaring and writing a special method called a constructor, in which you can perform initialization work for the object. Whenever an object is created, one of its constructors is executed.

C++ programmers often call a constructor a ctor.

Writing Constructors

Syntactically, a constructor is specified by a method name that is the same as the class name. A constructor never has a return type and may or may not have parameters. A constructor with no parameters is called the default constructor. There are many contexts in which you may have to provide a default constructor and you will get compiler errors if you have not provided one. Default constructors will be discussed later in the chapter.

Here is a first attempt at adding a constructor to the SpreadsheetCell class:

class SpreadsheetCell

{

public:

SpreadsheetCell(double initialValue);

// Remainder of the class definition omitted for brevity

};

Code snippet from SpreadsheetCellCtors\SpreadsheetCell.h

Just as you must provide implementations for normal methods, you must provide an implementation for the constructor:

SpreadsheetCell::SpreadsheetCell(double initialValue)

{

setValue(initialValue);

}

Code snippet from SpreadsheetCellCtors\SpreadsheetCell.cpp

The SpreadsheetCell constructor is a method of the SpreadsheetCell class, so C++ requires the normal SpreadsheetCell:: scope resolution before the method name. The method name itself is also SpreadsheetCell, so the code ends up with the funny looking SpreadsheetCell::SpreadsheetCell. The implementation simply makes a call to setValue() in order to set both the numeric and text representations.

Using Constructors

Using the constructor creates an object and initializes

Return Main Page Previous Page Next Page

®Online Book Reader