Thread: Best practice for postfix ++ operator return type

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well considering what Subsonics wrote
    Yes, but my understanding was that for chaining in general to work, you was required to return a reference. So, if my ++ operator did not return one it would affect other operators as well
    And considering the related code, your post was only tangentially related to why Subsonics approach would not work.

    Breaking down again how this works:
    cout << d++ << c;
    cout.operator<<(cout, d.operator++(int)).operator<<(cout, c);

    Probably not how it really folds out, but that's not the point.

    There is no situation where operator++ handles the chaining because cout -- an entirely different object -- is what is being "passed down" from operator to operator. So an in-depth explanation of chaining workings seemed off-topic to me. I hope that with my earlier post it became clear what d's role was in the chaining process, and how increment was really no part of it at all. So in this case, operator++(int) returning a reference or a value is neither here nor there. With this, I hope absolutely everyone understands.
    Last edited by whiteflags; 10-23-2010 at 07:03 PM.

  2. #17
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Ehm, so you are saying that this would work equally well.

    Code:
            void A::operator++(int){
                    b++;
                    updateA(b);
            }
    As far as cout is concerned?

    Anyway, I'll look to see if I can post something from operator<< as well. I just have to figure out what exactly to post, there are four class files affected, I think.

  3. #18
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Ehm, so you are saying that this would work equally well.
    No, because you want to output the returned value which this function doesn't have.

    The chaining is achieved by operator<< returning a reference to the left-hand stream, it has nothing to do with the values on the right side of the operator.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    More accurately, the chaining is achieved by the return value of the function, whatever that might be.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by anon View Post
    No, because you want to output the returned value which this function doesn't have.
    Yes, and that was exactly my point. And a reply to this:

    "There is no situation where operator++ handles the chaining because cout -- an entirely different object -- is what is being "passed down" from operator to operator. "

  6. #21
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >>Yes, and that was exactly my point.
    Anon did not say what you thought though.

    >>Ehm, so you are saying that this would work equally well. As far as cout is concerned?
    No.

    This is a typical implementation:
    Code:
    type operator++(int) // postfix increment
    {
       type val = impl;
       update(impl);
       return val;
    }
    The function now returns the value that you wanted to print, and updates it --

    cout << d;
    ++d;

    -- working just like the version without side-effects that uses prefix increment instead of postfix. So as far as cout is concerned, it prints the returned value of postfix increment. It is not chaining stream operations. That was all there was to notice in the example.

    A correct implementation of postfix increment might fix your other operators that are actually being chained. If it doesn't, then there is some other, compounding problem, and I don't know yet.

  7. #22
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    I don't really want to read all this, but the way you allow chaining with other operators is by returning a reference(&). So just take your declaration and add an ampersand to it's return type, like so...
    Code:
    type &operator++(int) // postfix increment
    {
       type val = impl;
       update(impl);
       return val;
    }

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You should read everything if you're going to enter the discussion.
    You do not actually need to return a reference to chain. You need an appropriate return type.

    Code:
    #include <iostream>
    
    class myobj
    {
    public:
    	myobj operator ++ (int) { myobj tmp(*this); ++myint; return tmp; }
    	int myint;
    };
    
    int main()
    {
    	int a, b, c;
    	myobj d;
    	d.myint = 1;
    	a = b = c = (d++).myint;
    	std::cout << a << b << c << d.myint << std::endl;
    	((d++)++)++;
    }
    Last edited by Elysia; 10-24-2010 at 11:56 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by User Name: View Post
    I don't really want to read all this, but the way you allow chaining with other operators is by returning a reference(&). So just take your declaration and add an ampersand to it's return type, like so...
    Code:
    type &operator++(int) // postfix increment
    {
       type val = impl;
       update(impl);
       return val;
    }
    The problem with that though is that it's returning a reference to a local variable.

  10. #25
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by whiteflags View Post
    >>Yes, and that was exactly my point.
    Anon did not say what you thought though.
    Ok, you tell me what I though he said then.


    Quote Originally Posted by whiteflags View Post
    This is a typical implementation:
    ...
    Yes, and that is exactly what I'm doing now.

    Quote Originally Posted by whiteflags View Post
    It is not chaining stream operations. That was all there was to notice in the example.

    A correct implementation of postfix increment might fix your other operators that are actually being chained. If it doesn't, then there is some other, compounding problem, and I don't know yet.
    Well it's part of the chain, clearly.

  11. #26
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >>Ok, you tell me what I though he said then.
    He was informing you that the function you had implemented returned no value. If it returns no value then cout has nothing to print. So what you wrote and what you thought I wanted you to do were not the same. If your point was that it returns no object for cout to print, and you were trying to follow my advice, then at that time you misunderstood me.

    >>Yes, and that is exactly what I'm doing now.
    It didn't look like it to me. But I'm forced to believe you're correct.

    >>Well it's part of the chain, clearly.
    In the same way that cout will always need to display things, sure. But the next operator is the next link in the chain, carrying the stream object cout and another object to display. Operations on d only affect d, and not what cout or its operators are doing later on.

  12. #27
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by whiteflags View Post
    >>Ok, you tell me what I though he said then.
    He was informing you that the function you had implemented returned no value. If it returns no value then cout has nothing to print. So what you wrote and what you thought I wanted you to do were not the same. If your point was that it returns no object for cout to print, and you were trying to follow my advice, then at that time you misunderstood me.
    Yes, but it did not return a value on purpose, to demonstrate how that effects the stream.

    Quote Originally Posted by whiteflags View Post
    >>Yes, and that is exactly what I'm doing now.
    It didn't look like it to me. But I'm forced to believe you're correct.
    Can you elaborate on this, I assign the current value to a temp variable, I then update *this and return the temp variable.

    Quote Originally Posted by whiteflags View Post
    >>Well it's part of the chain, clearly.
    In the same way that cout will always need to display things, sure. But the next operator is the next link in the chain, carrying the stream object cout and another object to display. Operations on d only affect d, and not what cout or its operators are doing later on.

    Yes, the increment operation on d has nothing to do with the stream. But if you think about what the difference is between post and prefix increment operations it also has an effect outside of d, in this case the output stream.

  13. #28
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Why are you trying to do this, you realise you can't do it with int, for the very reason that ++ with an int doesn't return a reference, it returns a copy. Also, "+= 2" is exactly the same amount of chars to type as "++++", and it looks much better.

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by User Name: View Post
    Why are you trying to do this, you realise you can't do it with int, for the very reason that ++ with an int doesn't return a reference, it returns a copy.
    We have already discussed this.
    It doesn't work not because it returns a copy, but because it returns a non-l-value.
    The ++ operator applied to ints require an l-value.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #30
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    A "++++" is not the same as "+= 2". Let a = 3 and evaluate (a++)++. First, a gets incremented to 4, but returns 3. The second post-increment would increment the 3 to a 4.
    Quote Originally Posted by pianorain View Post
    I don't see how chaining post-increments would be useful at all. Even if it was legal, it would only have the effect of a single post-increment.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined reference to.. probably Makefile problem
    By mravenca in forum C Programming
    Replies: 11
    Last Post: 10-20-2010, 04:29 AM
  2. How to pass a matrix/array from main to a function
    By Inukami in forum C Programming
    Replies: 7
    Last Post: 12-09-2009, 09:03 PM
  3. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  4. Segmentation Fault?
    By John_L in forum C Programming
    Replies: 10
    Last Post: 10-02-2007, 08:37 AM
  5. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM