PDA

View Full Version : Can you set a watch for std::cout in the debugger?




uvcoix
Nov 25, 2010, 01:36 PM
I'm relatively new to programming in C++.
Reading about standard input/output functionality in C++ I found that cout command is in fact an object of class ostream. If so, presumably one should be able to see this object initialized in the debugger mode.

I was wondering if it was possible to set a watch for std::cout in the xcode debugger (when running a simple "Hello World" programme for example)?
I haven't been able to locate it. Any help/clarification is very much appreciated.

thanks.



mrbash
Nov 25, 2010, 01:48 PM
The member you are probably interested in is static. No one creates an instance. They just use the overloaded operator for a particular function.

uvcoix
Nov 25, 2010, 02:05 PM
The member you are probably interested in is static. No one creates an instance. They just use the overloaded operator for a particular function.

Thanks a lot for a swift reply. Could you please explain what you mean by the statement "No one creates an instance" (I apologize if this sounds pedantic, but I'm trying to get to details).
I looked up in the debugger under "File Statics", but only found a variable std::__ioinit of type 'std::ios_base::Init' , am I looking in the wrong place?

Thanks!

Detrius
Nov 25, 2010, 06:50 PM
std::cout is an object. It is not a command. You can't set a breakpoint on an object. You can set a breakpoint on its initializer, but the fact that it's a static object means that it's initialized before main() is ever run. Additionally, there are likely a fair number of similar objects that are initialized as statics, so you'd have a difficult time knowing exactly which object is being initialized.

Probably the easiest thing to do is to use the debugger to step into operator<<() which will put you a file related to cout's object type. From there, you can follow the trail to find the initializer and put a breakpoint in that function.

uvcoix
Nov 26, 2010, 02:18 AM
std::cout is an object. It is not a command. You can't set a breakpoint on an object. You can set a breakpoint on its initializer, but the fact that it's a static object means that it's initialized before main() is ever run. Additionally, there are likely a fair number of similar objects that are initialized as statics, so you'd have a difficult time knowing exactly which object is being initialized.

Probably the easiest thing to do is to use the debugger to step into operator<<() which will put you a file related to cout's object type. From there, you can follow the trail to find the initializer and put a breakpoint in that function.

Thanks for the useful explanation! Indeed, after a bit of thought and playing around with the debugger, I've realized that your answer is correct and to the point. I did follow your suggestion and stepped into the operator<<(), and the std::cout object did materialize. As I understood, since std::cout object is created outside main(), it is not in scope while the programme execution is in main().

I would need to read up on scoping to understand the intricacies - But thanks a lot for your answer - it has set me on the right track!