In this section, we present the watch command, which tells the debugger to watch a data member. When that data member is about to change, the debugger will notify you. In this section, you will learn how to use the watch command to see how the Account object's data member balance is modified during the execution of the program.
1. |
Starting the debugger. Start the debugger by typing gdb figM_03. |
||
2. |
Running the program. Type break 14 to set a breakpoint at line 14. Run the program with the command run. The debugger and program will pause at the breakpoint at line 14 (Fig. M.26). Figure M.26. Running the program until the first breakpoint. (This item is displayed on page 1394 in the print version)
|
||
3. |
Watching a class's data member. Set a watch on account1's balance data member by typing watch account1.balance (Fig. M.27). This watch is labeled as watchpoint 2 because watchpoints are labeled with the same numbers as breakpoints. You can set a watch on any variable or data member of an object currently in scope during execution of the debugger. Whenever the value of a watched variable changes, the debugger enters break mode and notifies you that the value has changed. Figure M.27. Setting a watchpoint on a data member. (This item is displayed on page 1394 in the print version)
|
||
4. |
Continuing the program. Step into the Account constructor with the command step. The debugger will display line 12 of Fig. M.2, which is the first line in the constructor. Use the step command again to execute this line of code. The debugger will now notify you that data member balance's value will change (Fig. M.28). When the program begins, an instance of Account is created. When the constructor for this object runs, data member balance is first assigned the value 0. The debugger notifies you that the value of balance has been changed. Figure M.28. Stepping into the constructor.
|
||
5. |
Finishing the constructor. Type step to execute line 16, then type step again to execute line 17. The debugger will notify you that data member balance's value has changed from 0 to 50 (Fig. M.29). Figure M.29. Reaching a watchpoint notification.
|
||
|
|||
6. |
Withdrawing money from the account. Type continue to continue execution and enter a withdrawal value at the prompt. The program executes normally. Line 25 of Fig. M.3 calls Account member function debit to reduce the Account object's balance by a specified amount. Line 34 of Fig. M.2 inside function debit changes the value of balance. The debugger notifies you of this change and enters break mode (Fig. M.30). Figure M.30. Entering break mode when a variable is changed.
|
||
7. |
Continuing execution. Type continuethe program will finish executing function main because the program does not attempt any additional changes to balance. The debugger removes the watch on account1's balance data member because the variable goes out of scope when function main ends. Removing the watchpoint causes the debugger to enter break mode. Type continue again to finish execution of the program (Fig. M.31). Figure M.31. Continuing to the end of the program.
|
||
8. |
Restarting the debugger and resetting the watch on the variable. Type run to restart the debugger. Once again, set a watch on account1 data member balance by typing watch account1.balance. This watchpoint is labeled as watchpoint 3. Type continue to continue execution (Fig. M.32). Figure M.32. Resetting the watch on a data member.
|
||
9. |
Removing the watch on the data member. Suppose you want to watch a data member for only part of a program's execution. You can remove the debugger's watch on variable balance by typing delete 3 (Fig. M.33). Type continuethe program will finish executing without reentering break mode. Figure M.33. Removing a watch.
|
In this section, you learned how to use the watch command to enable the debugger to notify you of changes to the value of a data member throughout the life of a program. You also learned how to use the delete command to remove a watch on a data member before the end of the program.
Introduction to Computers, the Internet and World Wide Web
Introduction to C++ Programming
Introduction to Classes and Objects
Control Statements: Part 1
Control Statements: Part 2
Functions and an Introduction to Recursion
Arrays and Vectors
Pointers and Pointer-Based Strings
Classes: A Deeper Look, Part 1
Classes: A Deeper Look, Part 2
Operator Overloading; String and Array Objects
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
Templates
Stream Input/Output
Exception Handling
File Processing
Class string and String Stream Processing
Web Programming
Searching and Sorting
Data Structures
Bits, Characters, C-Strings and structs
Standard Template Library (STL)
Other Topics
Appendix A. Operator Precedence and Associativity Chart
Appendix B. ASCII Character Set
Appendix C. Fundamental Types
Appendix D. Number Systems
Appendix E. C Legacy Code Topics
Appendix F. Preprocessor
Appendix G. ATM Case Study Code
Appendix H. UML 2: Additional Diagram Types
Appendix I. C++ Internet and Web Resources
Appendix J. Introduction to XHTML
Appendix K. XHTML Special Characters
Appendix L. Using the Visual Studio .NET Debugger
Appendix M. Using the GNU C++ Debugger
Bibliography