views:

336

answers:

4

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 myClass::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!

+6  A: 

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.

Sebastian
Unfortunately, the suggestions from initial answers (b myClass ::operator()) does not work - see updated Q for error :(
DVK
Have you tried `b myClass::operator()(string)`?
Sebastian
A: 
b myClass::operator()
anon
Unfortunately, the suggestions from initial answers (b myClass ::operator()) does not work - see updated Q for error :(
DVK
Works for me. I tested it before I posted this answer.
anon
Could it be gdb version?
DVK
DVK -- Is your class defined inside a namespace?
Dan
@Dan - yes! "namespace {"
DVK
+5  A: 

gdb will also take breakpoints at specific line numbers. For example b file.cc:45

ezpz
A: 

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.

caspin