Online Book Reader

Home Category

Professional C__ - Marc Gregoire [374]

By Root 1303 0
functions. Now, consider pointers to class members and methods. It’s perfectly legitimate in C++ to take the addresses of class members and methods in order to obtain pointers to them. However, you can’t access a non-static member or call a non-static method without an object. The whole point of class members and methods is that they exist on a per-object basis. Thus, when you want to call the method or access the member via the pointer, you must dereference the pointer in the context of an object. Here is an example:

SpreadsheetCell myCell(123);

double (SpreadsheetCell::*methodPtr) () const = &SpreadsheetCell::getValue;

cout << (myCell.*methodPtr)() << endl;

Code snippet from PtrsToMethodsAndMembers\SpreadsheetCellTest.cpp

Don’t panic at the syntax. The second line declares a variable called methodPtr of type pointer to a non-static const method that takes no arguments and returns a double. At the same time, it initializes this variable to point to the getValue() method of the SpreadsheetCell class. This syntax is quite similar to declaring a simple function pointer, except for the addition of SpreadsheetCell:: before the *methodPtr. Note also that the & is required in this case.

The third line calls the getValue() method (via the methodPtr pointer) on the myCell object. Note the use of parentheses surrounding myCell.*methodPtr. They are needed because () has higher precedence than *.

Most of the time C++ programmers simplify the second line by using a typedef:

SpreadsheetCell myCell(123);

typedef double (SpreadsheetCell::*PtrToGet) () const;

PtrToGet methodPtr = &SpreadsheetCell::getValue;

cout << (myCell.*methodPtr)() << endl;

Code snippet from PtrsToMethodsAndMembers\SpreadsheetCellTest.cpp

Pointers to methods and members usually won’t come up in your programs. However, it’s important to keep in mind that you can’t dereference a pointer to a non-static method or member without an object. Every so often, you’ll find yourself wanting to try something like passing a pointer to a non-static method to a function such as qsort() that requires a function pointer, which simply won’t work .

Note that C++ permits you to dereference a pointer to a static member or method without an object.

SMART POINTERS


Memory management in C++ is a perennial source of errors and bugs. Many of these bugs arise from the use of dynamic memory allocation and pointers. When you extensively use dynamic memory allocation in your program and pass many pointers between objects, it’s difficult to remember to call delete on each pointer exactly once and at the right time. The consequences of getting it wrong are severe: When you free dynamically allocated memory more than once you can cause memory corruption or a fatal run-time error, and when you forget to free dynamically allocated memory you cause memory leaks.

Smart pointers help you manage your dynamically allocated memory and are the recommended technique for avoiding memory leaks. Smart pointers are a notion that arose from the fact that most memory-related issues are avoidable by putting everything on the stack. The stack is much safer than the heap because stack variables are automatically destructed and cleaned up when they go out of scope. Smart pointers combine the safety of stack variables with the flexibility of heap variables. There are several kinds of smart pointers. The most simple type of smart pointer takes sole ownership of the memory and when the smart pointer goes out of scope, it will free the referenced memory, for example, unique_ptr in C++11.

However, managing pointers presents more problems than just remembering to delete them when they go out of scope. Sometimes several objects or pieces of code contain copies of the same pointer. This problem is called aliasing. In order to free all memory properly, the last piece of code to use the memory should call delete on the pointer. However, it is often difficult to know which piece of code uses the memory last. It may even be impossible to determine the order when you code because it might depend on run-time

Return Main Page Previous Page Next Page

®Online Book Reader