Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

uvcoix

macrumors newbie
Original poster
Nov 25, 2010
3
0
UK
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

macrumors 6502
Aug 10, 2008
251
1
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

macrumors newbie
Original poster
Nov 25, 2010
3
0
UK
Thanks.

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

macrumors 68000
Sep 10, 2008
1,623
19
Apex, NC
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

macrumors newbie
Original poster
Nov 25, 2010
3
0
UK
thanks.

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!
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.