CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2007
    Posts
    18

    Smile operator<< overloading

    Hi forum

    Take this example code:

    Code:
    class Object
    {
    public:
    
    Object(const char *String);
    
    Object &operator<<(const Object &LHS);
    }
    
    Object A, B, C, D;
    
    A << B << "test" << C;
    What would the last statement be equivalent to?

    A)

    Code:
    A.operator<<(B);
    Object Temp("test");
    A.operator<<(Temp);
    A.operator<<(C);
    OR

    B)

    Code:
    Object Temp("test");
    A.operator<<( B.operator<<( Temp.Operator<<( C ) ) );
    I believe it is A, but why then do so many tutorials on operator<<-overloading say something like:

    "We have to return the [in this case class Object] object, since this allows us to chain multiple objects to be output on a single line (e.g.: A << "blah" << B << C; )."

    If the correct equivalent is A, this is wrong, because you don't NEED to return the object for that. You would only have to return it for things like:

    Code:
    if( (A << B << C) == D) return;
    Am I right or am I mistaken?

    Any help is highly appreciated

  2. #2
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: operator<< overloading

    It is equivalent to:
    Code:
    A.operator<<(B).operator<<("test").operator<<(C);


    Or in other words:

    Code:
    Object& ref1 = A.operator<<(B);
    Object& ref2 = ref1.operator<<("test");
    Object& ref3 = ref2.operator<<(C);
    My hobby projects:
    www.rclsoftware.org.uk

  3. #3
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: operator<< overloading

    Neiter A nor B, but
    Code:
    ((A.operator<<(B)).operator<<(Object("test"))).Operator<<(C);
    So it will run A << B, store the result in a temp and then run temp << Object("test"), and so on.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: operator<< overloading

    If the correct equivalent is A, this is wrong, because you don't NEED to return the object for that.
    You should test your hypothesis with an example program where operator<< returns void and yet you attempt operator chaining. After that contrapositive logic will show you that the correct equivalent is not A, as Zaccheus and treuss have noted.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jan 2007
    Posts
    18

    Re: operator<< overloading

    Thank you so much to all of you for your responses. You helped me a lot and I understand now why I was mistaken...



    Take care.

  6. #6
    Join Date
    May 2007
    Location
    Bangalore India
    Posts
    262

    Re: operator<< overloading

    Good that you understood..But I thing I still dont understand is why you have overloaded operator<< in the first place that way.
    Dont forget to rate my post if you find it useful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured