Question about the operator<<

Discussion in 'C++' started by Yudan Yi \(OSU\), Dec 7, 2005.

  1. I have a question to define a friend operator<< for a class.
    for example, I can define
    friend ostream& operator<<(ostream& os, const TTest& x) { ...; return
    (os); };

    While I want to add more control to the output by an additional parameter,
    can I do in the following way?
    friend ostream& operator<<(ostream& os, const TTest& x, unsigned parameter)
    {
    switch (parameter)
    {
    case: ...
    }
    return (os);
    };

    If I can define in this way, then how can I use it? if not, how can I do it?

    Thanks
     
    Yudan Yi \(OSU\), Dec 7, 2005
    #1
    1. Advertising

  2. On Wed, 7 Dec 2005 16:43:42 -0500, "Yudan Yi \(OSU\)" <>
    wrote:

    >I have a question to define a friend operator<< for a class.
    >for example, I can define
    >friend ostream& operator<<(ostream& os, const TTest& x) { ...; return
    >(os); };
    >
    >While I want to add more control to the output by an additional parameter,
    >can I do in the following way?
    >friend ostream& operator<<(ostream& os, const TTest& x, unsigned parameter)
    >{
    > switch (parameter)
    > {
    > case: ...
    > }
    > return (os);
    >};
    >
    >If I can define in this way, then how can I use it? if not, how can I do it?


    You can't change the signature of the function because it won't work
    otherwise. How would you call it?

    Maybe you can make the parameter part of TTest's class, in which case
    you can test it inside the body of the operator. Since it is a friend
    of TTest, it has access to all of its private and protected members.

    --
    Bob Hairgrove
     
    Bob Hairgrove, Dec 7, 2005
    #2
    1. Advertising

  3. Yudan Yi \(OSU\)

    Guest

    Yudan Yi (OSU) wrote:
    > I have a question to define a friend operator<< for a class.
    > for example, I can define
    > friend ostream& operator<<(ostream& os, const TTest& x) { ...; return
    > (os); };
    >
    > While I want to add more control to the output by an additional parameter,
    > can I do in the following way?


    No. << is a binary operator.

    > friend ostream& operator<<(ostream& os, const TTest& x, unsigned parameter)
    > {
    > switch (parameter)
    > {
    > case: ...
    > }
    > return (os);
    > };
    >
    > If I can define in this way, then how can I use it? if not, how can I do it?
    >


    Define a function that takes three parameters and call it.
     
    , Dec 7, 2005
    #3
  4. Yudan Yi (OSU) wrote:
    > While I want to add more control to the output by an additional parameter,
    > can I do in the following way?


    No: the 'operator<<()' is a binary function. Assuming you want to
    add some form of format control for 'TTest' to your stream, you
    can store the format using the 'iword()' or 'pword()' members of
    the stream. This could look e.g. like this:

    int key = std::eek:stream::xalloc();

    std::eek:stream& mode1(std::eek:stream& out) {
    out.iword(key) = 1;
    return out;
    }
    std::eek:stream& mode2(std::eek:stream& out) {
    out.iword(key) = 2;
    return out;
    }
    // ...

    friend ostream& operator<<(ostream& os, TTest const& x)
    {
    switch (os.iword(key))
    {
    case 1: // ...
    case 2: // ...
    case 0: // this is the initial value of 'os.iword(key)'
    default: // ...
    }
    return out;
    }

    You would use it like this:

    void print(TTest const& x) {
    std::cout << "default format: " << x << "\n";
    std::cout << "format 1: " << mode1 << x << "\n";
    std::cout << "format 2: " << mode2 << x << "\n";
    }
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    <http://www.eai-systems.com> - Efficient Artificial Intelligence
     
    Dietmar Kuehl, Dec 7, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jakob Bieling

    Q: operator void* or operator bool?

    Jakob Bieling, Mar 5, 2004, in forum: C++
    Replies:
    2
    Views:
    602
    Rob Williscroft
    Mar 5, 2004
  2. John Smith
    Replies:
    2
    Views:
    436
    Ivan Vecerina
    Oct 6, 2004
  3. Alex Vinokur
    Replies:
    4
    Views:
    3,064
    Peter Koch Larsen
    Nov 26, 2004
  4. Alex Vinokur
    Replies:
    3
    Views:
    5,046
    Jeff Schwab
    Mar 20, 2005
  5. Tim Clacy
    Replies:
    15
    Views:
    2,722
    Kanenas
    May 30, 2005
Loading...

Share This Page