1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

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

Discussion in 'Mac Programming' started by uvcoix, Nov 25, 2010.

  1. macrumors newbie

    #1
    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.
     
  2. macrumors 6502

    #2
    The member you are probably interested in is static. No one creates an instance. They just use the overloaded operator for a particular function.
     
  3. macrumors newbie

    #3
    Thanks.

    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!
     
  4. macrumors 68000

    #4
    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.
     
  5. uvcoix, Nov 26, 2010
    Last edited: Nov 26, 2010

    macrumors newbie

    #5
    thanks.

    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!
     

Share This Page