Online Book Reader

Home Category

Professional C__ - Marc Gregoire [322]

By Root 1250 0
comparison you want.

Of course, you could implement either of the preceding benefits with global or static variables. However, function objects provide a cleaner way to do it, and using global or static variables might cause problems in a multithreaded application. The true benefits of function objects are demonstrated with the STL in Chapter 13.

By following the normal method overloading rules, you can write as many operator()s for your classes as you want. Specifically, the various operator()s must have a different number of parameters or different types of parameters. For example, you could add an operator() to the FunctionObject class that takes a string reference:

int operator() (int inParam);

void operator() (string& str);

int doSquare(int inParam);

Code snippet from Functors\Functors.cpp

The function call operator can also be used to provide subscripting for multiple indices of an array. Simply write an operator() that behaves like operator[] but allows more than one parameter. The only problem with this technique is that now you have to use () to index instead of [], as in myArray(3, 4) = 6;

OVERLOADING THE DEREFERENCING OPERATORS


You can overload three de-referencing operators: *, ->, and ->*. Ignoring ->* for the moment (we’ll get back to it later), consider the built-in meanings of * and ->. * dereferences a pointer to give you direct access to its value, while -> is shorthand for a * dereference followed by a . member selection. The following code shows the equivalences:

SpreadsheetCell* cell = new SpreadsheetCell;

(*cell).set(5); // Dereference plus member selection

cell->set(5); // Shorthand arrow dereference and member selection together

You can overload the dereferencing operators for your classes in order to make objects of the classes behave like pointers. The main use of this capability is for implementing smart pointers, introduced in Chapter 1 and discussed in detail in Chapter 21. It is also useful for iterators, which the STL uses, discussed in Chapter 12. This chapter teaches you the basic mechanics for overloading the relevant operators in the context of a simple smart pointer template class.

Chapter 21 discusses in detail two standard C++11 smart pointers called std::shared_ptr and std::unique_ptr. It is highly recommended to use these standard smart pointer classes instead of writing your own. The example here is given only to demonstrate how to write dereferencing operators.

Here is the example smart pointer template class definition, without the dereference operators filled in yet:

template

class Pointer

{

public:

Pointer(T* inPtr);

virtual ~Pointer();

// Dereference operators will go here.

protected:

T* mPtr;

private:

// Prevent assignment and pass by value.

Pointer(const Pointer& src);

Pointer& operator=(const Pointer& rhs);

};

Code snippet from DereferenceOps\Pointer.h

This smart pointer is about as simple as you can get. All it does is store a dumb pointer, and the storage pointed to by the pointer is deleted when the smart pointer is destroyed. The implementation is equally simple: The constructor takes a real (“dumb”) pointer, which is stored as the only data member in the class. The destructor frees the storage referenced by the pointer:

template

Pointer::Pointer(T* inPtr)

{

mPtr = inPtr;

}

template

Pointer::~Pointer()

{

delete mPtr;

mPtr = nullptr;

}

Code snippet from DereferenceOps\Pointer.h

You would like to be able to use the smart pointer template like this:

Pointer smartInt(new int);

*smartInt = 5; // Dereference the smart pointer.

cout << *smartInt << endl;

Pointer smartCell(new SpreadsheetCell);

smartCell->set(5); // Dereference and member select the set method.

cout << smartCell->getValue() << endl;

Code snippet from DereferenceOps\PointerTest.cpp

As you can see from this example, you will have to provide implementations of operator* and operator-> for this class. These will be implemented in the next two sections.

You should rarely,

Return Main Page Previous Page Next Page

®Online Book Reader