United States    
COMPAQ STORE | PRODUCTS |
SERVICES | SUPPORT | CONTACT US | SEARCH
cxxtitle.gif (12116 bytes)
Compaq C++

Compaq C++
Using Compaq C++ for Tru64 UNIX Systems


Previous Contents Index

8.7 Modifying Class and Object Data Members

When debugging C++ code, the debugger lets you modify a class's static data members and object data members as you would modify any other program variable by using the debugger's assign command. See the Tru64 UNIX Ladebug Debugger Manual for details on how to use the assign command to modify variable values. The following assignments are allowed by the debugger even though they are prohibited in the C++ language:

8.8 Member Functions on the Stack Trace

The implicit this pointer, which is a part of all nonstatic member functions, is displayed as the address on the stack trace. The class type of the object is also given.

Sometimes the debugger does not see class type names with internal linkage. When this happens, the debugger gives the following error message:


Name is overloaded. 

The stack trace in Figure 8-1 displays a member function foo of an object declared with class type S.

Figure 8-1 A Stack Trace Displaying a Member Function


8.9 Resolving Ambiguous References to Overloaded Functions

In most cases, the debugger works with one specific function at a time. In the case of overloaded function names, you must specify the desired overloaded function. There are two ways to resolve references to overloaded function names. Both ways are under the control of the debugger variable $overloadmenu. The default setting of this debugger variable is 0.

One way to specify the desired function name is to choose the correct reference from a selection menu. To enable menu selection of overloaded names, set the $overloadmenu variable to 1. If you use this method, whenever you specify a function name that is overloaded, a menu will appear with all the possible functions; you must select from this menu. In Example 8-7, a breakpoint is set in foo, which is overloaded.

Example 8-7 Resolving Overloaded Functions by Selection Menu

(Ladebug) set $overloadmenu = 1
(Ladebug) class S
class S  { 
  int i; 
  float f; 
  double d; 
  char c; 
  S (void); 
  ~S (void); 
  int foo (void); 
  int foo ( S); 
  int foo ( S *); 
  int foo (int&); 
  int foo (const int&); 
  int foo (float*&); 
  int foo (int, float, char *, unsigned short, long&, const char*&); 
  int foo (const double *); 
  int foo (char); 
  void foo (short); 
  void foo (unsigned); 
  void foo (long); 
  void foo (int, float, char *, unsigned short, long&, char*&); 
  void foo (double); 
  void foo (char *); 
  void foo (const char *); 
} 
(Ladebug) stop in foo
Enter the number of the overloaded function you want 
---------------------------------------------------- 
     1 int foo (void) 
     2 void foo (const char *) 
     3 void foo (char *) 
     4 void foo (double) 
     5 void foo (int, float, char *, unsigned short, long&, char*&) 
     6 void foo (long) 
     7 void foo (unsigned) 
     8 void foo (short) 
     9 int foo (char) 
    10 int foo (const double *) 
    11 int foo (int, float, char *, unsigned short, long&, const char*&) 
    12 int foo (float*&) 
    13 int foo (const int&) 
    14 int foo (int&) 
    15 int foo ( S *) 
    16 int foo ( S) 
    17 None of the above 
---------------------------------------------------- 
10
[#1: stop in int S::foo(const double*) ] 
(Ladebug) 

The other way to resolve ambiguous overloaded function names is to enter the function name with its full type signature. If you prefer this method, set the $overloadmenu variable to 0. To see the possible type signatures for the overloaded function, first display all the declarations of an overloaded function by using one of the following syntax lines:
whatis function
whatis class_name::function

You cannot select a version of an overloaded function that has a type signature containing ellipsis points. Pointers to functions with type signatures that contain parameter list or ellipsis arguments are not supported.

Use one of the displayed function type signatures to refer to the desired version of the overloaded function. If a function has no parameter, include the parameter void as the function's type signature. In Example 8-8 the function context is set to foo(), which is overloaded.

Example 8-8 Resolving Overloaded Functions by Type Signature

(Ladebug) print $overloadmenu
0 
(Ladebug) class S
class S  { 
  int i; 
  float f; 
  double d; 
  char c; 
  S (void); 
  ~S (void); 
  int foo (void); 
  int foo ( S); 
  int foo ( S *); 
  int foo (int&); 
  int foo (const int&); 
  int foo (float*&); 
  int foo (int, float, char *, unsigned short, long&, const char*&); 
  int foo (const double *); 
  int foo (char); 
  void foo (short); 
  void foo (unsigned); 
  void foo (long); 
  void foo (int, float, char *, unsigned short, long&, char*&); 
  void foo (double); 
  void foo (char *); 
  void foo (const char *); 
} 
(Ladebug) whatis foo
Overloaded Function. Functions are: 
int S::foo(void) 
int S::foo(S) 
int S::foo(S*) 
int S::foo(int&) 
int S::foo(const int&) 
int S::foo(float*&) 
int S::foo(int, float, char*, unsigned short, long&, const char*&) 
int S::foo(const double*) 
int S::foo(char) 
void S::foo(short) 
void S::foo(unsigned) 
void S::foo(long) 
void S::foo(int, float, char*, unsigned short, long&, char*&) 
void S::foo(double) 
void S::foo(char*) 
void S::foo(const char*) 
(Ladebug) func foo
Error: foo is overloaded 
(Ladebug) func foo(double)
S::foo(double) in c++over.C line No. 156: 
    156     printf ("void S::foo (double d = %f)\n", d); 
(Ladebug) 

8.10 Setting Breakpoints in Member Functions

When you set a breakpoint in a C function, the debugger confirms the breakpoint by echoing the breakpoint command along with the status number for the breakpoint. When you set a breakpoint in a C++ function, the debugger also prints the type signature of the function in which the breakpoint was set.

To set a breakpoint that stops in a member function, use one of the following syntax lines:
stop in function
stop in class_name::function

This form of specifying a breakpoint in a function uses the static class type information to determine the address of the function at which to set the breakpoint, and presumes that no run-time information from an object is needed.

In Example 8-9 a breakpoint is set for member function bar of class S.

Example 8-9 Setting Breakpoints in Member Functions

(Ladebug) stop in S :: bar
[#1: stop in S::bar(void) ] 
(Ladebug) status
#1 PC==0x120000658 in S::bar(void) "c++ex.C":18 { break } 
(Ladebug) run
[1] stopped at [S::bar(void):18 0x120000658]       
     18      return j; 
(Ladebug) where
>0  0x120000658 in ((S*)0x120000658)->bar() c++ex.C:18 
#1  0x120000750 in main() c++ex.C:26 
(Ladebug) 

If you need run-time information from the object to determine the correct virtual function at which to set a breakpoint, qualify the function name with the object using one of the following syntax lines:
stop in object.function
stop in objectpointer->function

Setting the breakpoint in this way causes the debugger to stop at the member function in all objects declared with the same class type as the specified object. In Example 8-10, objects s and t are both declared to be of class type S. A breakpoint is set for member function bar. The first time the debugger stops at bar() is for object s. The second time the debugger stops in bar() for object t.

Example 8-10 Setting Breakpoints in Virtual Member Functions

(Ladebug) stop in main
[#1: stop in main(void) ] 
(Ladebug) run
[1] stopped at [main(void):26 0x120000744] 
     26      int result = s.bar(); 
(Ladebug) stop in s.bar
[#2: stop in S::bar(void) ] 
(Ladebug) status
#1 PC==0x120000744 in main(void) "c++ex.C":26 { break } 
#2 PC==0x120000658 in S::bar(void) "c++ex.C":18 { break } 
(Ladebug) print &s
0x140000000 
(Ladebug) print &t
0x140000008 
(Ladebug) cont
[2] stopped at [S::bar(void):18 0x120000658]       
     18      return j; 
(Ladebug) where
>0  0x120000658 in ((S*)0x140000000)->bar() c++ex.C:18 
#1  0x120000750 in main() c++ex.C:26 
(Ladebug) cont
[2] stopped at [S::bar(void):18 0x120000658]       
     18      return j; 
(Ladebug) where
>0  0x120000658 in ((S*)0x140000008)->bar() c++ex.C:18 
#1  0x12000076c in main() c++ex.C:27 
(Ladebug) 

To set a breakpoint that stops only in the member function for this specific object and not all instances of the same class type, you must specify this as an additional conditional clause to the stop command. Use one of the following syntax lines:
stop in object.function if & object :=,= this
stop in objectpointer->function if & objectpointer :=,= this

This form of the breakpoint command instructs the debugger to stop in the function only for the object specified by the this pointer. In Example 8-11, which is running the same program as Example 8-10, the breakpoint is set for the member function for object s only. After stopping in bar() for object s, further execution of the program results in the program running to completion.

Example 8-11 Setting Breakpoints in Member Functions for a Specific Object

(Ladebug) stop in s.bar if &s==this
[#2: stop in s.bar if &s==this ] 
(Ladebug) status
#1 PC==0x120000744 in main(void) "c++ex.C":26 { break } 
#2 (PC==0x120000658 in S::bar(void) "c++ex.C":18 and if &s==this) {break} 
(Ladebug) print &s
0x140000000 
(Ladebug) cont
[2] stopped at [S::bar(void):18 0x120000658]       
     18      return j; 
(Ladebug) where
>0  0x120000658 in ((S*)0x10000010)->bar() c++ex.C:18 
#1  0x120000750 in main() c++ex.C:26 
(Ladebug) cont
Thread has finished executing 
(Ladebug) 

8.10.1 Setting Breakpoints in Overloaded Functions

To set a breakpoint in an overloaded function, you must provide the full type signature of the function. Use one of the following command syntax lines:
stop in function (type_signature)
stop in class_name::function (type_signature)

If the desired version of the function has no parameters, you must enter void for the type signature. In Example 8-12 the breakpoint is set for specific versions of the overloaded function foo.

Example 8-12 Setting Breakpoints in Specific Overloaded Functions

(Ladebug) class S
class S  { 
  int i; 
  float f; 
  double d; 
  char c; 
  S (void); 
  ~S (void); 
  int foo (void); 
  int foo ( S); 
  int foo ( S *); 
  int foo (int&); 
  int foo (const int&); 
  int foo (float*&); 
  int foo (int, float, char *, unsigned short, long&, const char*&); 
  int foo (const double *); 
  int foo (char); 
  void foo (short); 
  void foo (unsigned); 
  void foo (long); 
  void foo (int, float, char *, unsigned short, long&, char*&); 
  void foo (double); 
  void foo (char *); 
  void foo (const char *); 
} 
(Ladebug) whatis foo
Overloaded Function. Functions are: 
int S::foo(void) 
int S::foo(S) 
int S::foo(S*) 
int S::foo(int&) 
int S::foo(const int&) 
int S::foo(float*&) 
int S::foo(int, float, char*, unsigned short, long&, const char*&) 
int S::foo(const double*) 
int S::foo(char) 
void S::foo(short) 
void S::foo(unsigned) 
void S::foo(long) 
void S::foo(int, float, char*, unsigned short, long&, char*&) 
void S::foo(double) 
void S::foo(char*) 
void S::foo(const char*) 
(Ladebug) stop in foo(double)
[#1: stop in void S::foo(double) ] 
(Ladebug) stop in foo(void)
[#2: stop in int S::foo(void) ] 
(Ladebug) status
#1 PC==0x120001508 in void S::foo(double) "c++over.C":156 { break } 
#2 PC==0x120000ef4 in int S::foo(void) "c++over.C":59 { break } 
(Ladebug) 

To set a breakpoint that stops in all versions of an overloaded function, use one of the following syntax lines:
stop in all function
stop in all class_name::function

In Example 8-13 the breakpoint is set for all versions of the overloaded function foo.

Example 8-13 Setting Breakpoints in All Versions of an Overloaded Function

(Ladebug) class S
class S  { 
  int i; 
  float f; 
  double d; 
  char c; 
  S (void); 
  ~S (void); 
  int foo (void); 
  int foo ( S); 
  int foo ( S *); 
  int foo (int&); 
  int foo (const int&); 
  int foo (float*&); 
  int foo (int, float, char *, unsigned short, long&, const char*&); 
  int foo (const double *); 
  int foo (char); 
  void foo (short); 
  void foo (unsigned); 
  void foo (long); 
  void foo (int, float, char *, unsigned short, long&, char*&); 
  void foo (double); 
  void foo (char *); 
  void foo (const char *); 
} 
(Ladebug) stop in all foo
[#1: stop in all foo ] 
(Ladebug) 

You can also set a breakpoint in an overloaded function by setting a breakpoint at the line number where the function begins. Be sure the current file context points to the file containing the function's source code before setting the breakpoint. In Example 8-14 the breakpoint is set for the overloaded functions by line number.

Example 8-14 Setting Breakpoints in Overloaded Functions by Line Number

(Ladebug) stop at 59
[#1: stop at "c++over.C":59 ] 
(Ladebug) stop at 156
[#2: stop at "c++over.C":156 ] 
(Ladebug) status
#1 PC==0x120000ef4 in S::foo(void) "c++over.C":59 { break } 
#2 PC==0x120001508 in S::foo(double) "c++over.C":156 { break } 
(Ladebug) 

8.10.2 Setting Breakpoints in Constructors and Destructors

To set a breakpoint in a constructor, use one of the following syntax lines:
stop in class_name::class_name [(type_signature)]
stop in class_name [(type_signature)]

The type signature is necessary only to resolve the ambiguity for a constructor that is overloaded. In Example 8-15, a breakpoint is set in a constructor.

Example 8-15 Setting Breakpoints in Constructors

(Ladebug) class S
class S  { 
  int i; 
  int j; 
  S (void); 
  ~S (void); 
  int foo (void); 
  virtual int bar (void); 
} 
(Ladebug) stop in S
[#1: stop in S::S(void) ] 
(Ladebug) status
#1 PC==0x1200005b8 in S::S(void) "c++ex.C":5 { break } 
(Ladebug) 

You can similarly set a breakpoint in a destructor using the following syntax:
stop in ~class_name

In Example 8-16, the breakpoint is set for the destructor.

Example 8-16 Setting Breakpoints in Destructors

(Ladebug) stop in  S
[#1: stop in ~S::S(void) ] 
(Ladebug) status
#1 PC==0x1200005f8 in S::~S(void) "c++ex.C":6 { break } 
(Ladebug) 

As with any function's type signature specification, constructors and destructors that have no parameters must be referenced with a type signature of void.

8.11 Calling Overloaded Functions

To call overloaded functions from the debugger, you must set $overloadmenu to 1. Then, use the following call command syntax:
call function ([parameter[,...]])

The debugger will call the function that you select from the menu of overloaded names. In Example 8-17 the overloaded function foo is called.

Example 8-17 Calling an Overloaded Function

(Ladebug) set $overloadmenu = 1
(Ladebug) call foo(15)
Enter the number of the overloaded function you want 
---------------------------------------------------- 
     1 void foo (const char *) 
     2 void foo (char *) 
     3 void foo (double) 
     4 void foo (float) 
     5 void foo (unsigned) 
     6 void foo (long) 
     7 void foo (short) 
     8 int foo (const double *) 
     9 int foo (int, float) 
    10 int foo (float*&) 
    11 int foo (const int&) 
    12 int foo (int&) 
    13 int foo (char) 
    14 int foo ( S *) 
    15 int foo ( S) 
    16 None of the above 
---------------------------------------------------- 
6
global foo (long):              15 
(Ladebug) 

8.12 Using Typecasts to Display Program Expressions

When debugging C++ programs, the debugger interprets casts as defined in The Annotated C++ Reference Manual. A cast has the following syntax:

(type) variable

The type is the type the debugger will use to interpret the program variable variable. The debugger does not allow cast conversion from an unsigned short type to a long int type.

In Example 8-18, a variable of type float is interpreted as an integer.

Example 8-18 Using a Cast to Perform Data Coercion

(Ladebug) whatis f
float f 
(Ladebug) print f
3.2 
(Ladebug) print (int)f
3 
(Ladebug) 

You can also use casts to interpret a base class object as a derived class object, or to interpret a derived class object as a base class object. In Example 8-19, the class derived inherits the public members of class base. A pointer to an object of class derived is declared to be a pointer to an object of class base. A cast is used to display the object's contents using the correct class type.

Example 8-19 Using Casts on a Derived Class Object

(Ladebug) whatis bvar
 base * bvar 
(Ladebug) whatis derived
class derived : base { 
  int i; 
  derived (void); 
  ~derived (void); 
  virtual int foo (void); 
} derived 
(Ladebug) whatis base
class base  { 
  int j; 
  base (void); 
  ~base (void); 
  virtual int foo (void); 
} base 
(Ladebug) print *bvar
class { 
        j = 0; 
    } 
(Ladebug) print *(derived *)bvar
class { 
        base = class { 
            j = 0; 
        }; 
        i = 4195376; 
    } 
(Ladebug) 

Example 8-20 shows how to use a type transfer to interpret a variable of type float as an integer.

Example 8-20 Using a Cast with Pointer Notation to Perform a Type Transfer

(Ladebug) print &f
0x11ffffe84 
(Ladebug) print *(int *)&f
1078774989 
(Ladebug) 

You can also perform the type transfer shown in this example by using C++ reference types. Example 8-21 shows how to use reference type notation to perform a type transfer.

Example 8-21 Using a Cast with Reference Type Notation to Perform a Type Transfer

(Ladebug) print &f
0x11ffffe84 
(Ladebug) print (int&)f
1078774989 
(Ladebug) 


Previous Next Contents Index
  

1.800.AT.COMPAQ

privacy and legal statement