How do I set a breakpoint for operator() in gdb for C++?
I have 2 methods in C++ class as follows:
class myClass {
public:
void operator()( string myString ) {
// Some code
}
void myMethod() { ... }
}
For a regular method, I can simply set the breakpoint in GDB as:
b myC开发者_C百科lass::myMethod
But how do I set the breakpoint for the first method?
UPDATE:
The suggestions from initial answers (b myClass ::operator()) does not work :(
b myClass::operator()
Function "myClass::operator()" not defined.
Thanks!
gdb will also take breakpoints at specific line numbers. For example
b file.cc:45
Just the same. myClass::operator()(string)
is a regular method.
If you have several overloaded operator()
methods (e.g. a const and a non-const version) gdb should offer the choice where to set the breakpoint:
http://sunsite.ualberta.ca/Documentation/Gnu/gdb-5.0/html_node/gdb_35.html#SEC35
You may have to make sure that method operator()(string)
is actually compiled.
Edit:
I've tested the following file test.cpp:
#include <string>
#include <iostream>
class myClass {
public:
void operator()( int i ) {
std::cout << "operator()";
}
void myMethod() {
std::cout << "myMethod";
}
};
int main() {
myClass c;
c(1);
c.myMethod();
return 0;
}
Compiled with g++ test.cpp -o test
, ran gdb test
(version GNU gdb 6.3.50-20050815 (Apple version gdb-1344)), typed start
and only then could I set breakpoints.
b 'myClass::operator()(string)'
and
b myClass::operator()
both worked.
Some C++ functions names can be really hard to type out correctly. Worse yet, gdb's autocompletion often gets confused with c++ names. I use this trick
gdb> break 'myClass::operator()<TAB>
Note the single quote at the beginning of the function. That helps gdb's autocompleter.
b myClass::operator()
精彩评论