Thread: Best practice for postfix ++ operator return type

  1. #31
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    @User Name:

    Can you please read the thread. I'm not trying to do var++++, I originally asked for a "best practice" regarding returning a reference for the sake of chaining operators. That has changed a bit but.

  2. #32
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    >>Can you elaborate on this, I assign the current value to a temp variable, I then update *this and return the temp variable.
    Sure, read below:

    >>Yes, but it did not return a value on purpose, to demonstrate how that effects the stream.
    >>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.
    Different strokes I guess. The object changes, not cout, until cout performs its operation. I didn't anticipate your conclusion. So it's all still a misunderstanding on our part and we could argue about the one true way. I don't want to.

  3. #33
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I believe you asked for advice concerning how to do it.
    Apparently it didn't work for some of your chaining.
    Regardless, there is usually only one "correct" way of doing post increment, and it is to return a copy.
    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.

  4. #34
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Anyway, thanks to everyone who have been participating here. I need to find this error elsewhere, what ever it is, it's not related to the cout requiring a reference.

  5. #35
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Refer to wikipedia here and you cant go wrong.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #36
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    no match for operator<< in operator++(int)(0)
    I'm willing to bet that that is not the entire error message.

    If it is, use a different compiler to get a different error message.

    GCC is a marvelous compiler, but it doesn't always produce the best error messages.

    I need to find this error elsewhere, what ever it is, it's not related to the cout requiring a reference.
    This is correct, but the error is absolutely related to your not providing an appropriate stream operator for the given context.

    I believe that your stream operator looks like `std:stream & operator << (std:stream &, Type &)' which is wrong.

    Soma

  7. #37
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Thanks iMalc, that is very useful.

    @phantomotap

    The stream works if I do like this for example:

    Code:
    A a, b;
    std::cout << a << (a - b) << (a == b);
    But not for post increment

    It's declared like this:

    Code:
    friend std::ostream & operator<<(std::ostream & os, Type & d);

  8. #38
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    It's declared like this
    Yes. That's what I said I believed it to be.

    As I said, that is wrong.

    Soma

  9. #39
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by phantomotap View Post
    Yes. That's what I said I believed it to be.

    As I said, that is wrong.

    Soma
    No. Yours was not declared as a friend. The stream works for printing the objects as I showed in the previous post. Why would it not work when the postincrement operator returns an instance of the object?

  10. #40
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Subsonics
    The stream works for printing the objects as I showed in the previous post. Why would it not work when the postincrement operator returns an instance of the object?
    It is wrong because it is not const-correct: the overloaded operator<< should not be modifying the observable state of the object, hence the type of the second parameter should be const Type&, not Type&. The problem that you observed in failing to be const-correct is that when your postfix operator++ returns by value, the temporary returned cannot be bound to this non-const reference parameter.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #41
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Thank you laserlight, for pointing out what is wrong. If I remember correctly I used to have it declared as const but that gave me errors elsewhere. Let me get back once I have tried to compile with the new const in place.

  12. #42
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Okay so inside the implementation of operator<< I use three get methods to display private data members. But I got a complaint about discarding const qualifiers with virtual methods. Now I tried to just display the data members directly since operator<< is declared as friend, and it worked. Don't know if that is considered ok, but I suppose so. Thanks again, now the postfix operator is working as it should without returning a reference.

  13. #43
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Discarding const usually means a const method is trying to call a non-const method. Also consider that if you invoke methods on a const object (eg a const reference to T), then all members are considered const.
    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.

  14. #44
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Thanks Elysia. I'm inspecting that right now, after compiling a test file that generated quite a few of these errors. So, adding const to these methods should solve the problem then?

  15. #45
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you forgot to add const to method that do not change the class's member variables, then yes, it will solve it. But if that isn't true, then you may have to evaluate your const chain.
    If a method calls a non-const method, then it make take a non-const reference of any arguments it intend to modify, in the present state or the future.
    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.

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