Thread: Returned value with operator overloaded function???

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    167

    Returned value with operator overloaded function???

    There are binary operators and unary operators. I am a little confused about the parameters and the return value of operators. For instance "+" takes two arguments right? The left operand and the right operand and the value returned is usually assigned to a variable. Hmm this makes a little sense but what about the "+=" operator. This takes two arguments also? Where does the return value go?

    An example from the book I'm reading.

    Code:
     
    class String { 
             friend const String &operator+=( String &, const String & );
    The += operator is being overloaded to concatenate two strings. The operand on the left is going to be modified so I understand why its declared const but I don't understand why the function is returning a const reference to a string. Where does the return value go anyway?
    silk.odyssey

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    The constant reference IS the return value. The reference so it doesn't have to be copied, and the const so the compiler can make optimizations (and disallow modifications of the reference).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    167
    string2 += string1;

    isn't string2 the left operand and string1 the right operand? If so where does the return value go?
    silk.odyssey

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    string2 is the first argument, string1 is the second. In your example (string2 += string1) you don't use the return value, but imagine a statement like this:

    string3 = string2 += string1

    stirng3 gets the return value. Confusing to read perhaps, but valid code. A more useful case would be the return value of the normal assignment, so you can nest like this:

    var1 = var2 = var3 = 4

    I never use that, but it's probably common for some poeple.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    167
    Magos,

    String3 is constant and string2 is not but they have the same value correct? What good reasons are there for having a constant return value for the function? What exactly is constant by the way? If the string was declared constant then it would not be possible to assign a value to it so what exactly is constant?
    Last edited by silk.odyssey; 05-08-2004 at 05:48 PM.
    silk.odyssey

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I haven't metioned anything about constants. Of course you cannot assign anything to a constant variable. The compiler (assumin it's a decent one) should complain about that. The reason an assignment should return the assigned value is cause the standard says so. Some smart guy(s) thought that out. The reasoning behind that I don't know, but it's probably so you can nest assignments (a = b = c) or use this kind of if statements:

    if((a = new int) == NULL)

    I never use nested assignments, I thnk it's too cluttered, especially when operations are performed too (+=,-= etc). It's a personal preference.

    EDIT: Oh, right. The reason the return value is constant is cause it's a reference. The reference is there so you don't have to copy the data, which would be more time consuming than simply passing a reference (kind of a pointer) to it. The const is there so you're not allowed to modify what is returned. It wouldn't be so good if you type a = b, then a can modify b through the return value from b. Note that a does NOT have to be a constant variable. You can always assign a normal variable the value of a constant variable.
    Last edited by Magos; 05-09-2004 at 04:16 AM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User
    Join Date
    Dec 2003
    Posts
    167
    I'm not sure I understand. After a = b, a points to the same value as b but what exactly is constant, the reference itself or the value the reference references?
    silk.odyssey

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    string3 = string2 += string1
    Using the example Magos provided... the returned value of the += operation (the new combined string) is the constant and is essentially a temporary in this sense. As soon as the value is returned, it is then used to initialize the string3 variable (by way of either the assignment operator or copy constructor).

    The returned value is defined to be a constant because there shouldn't be any real reason to need to modify it. In the above case, the constant returned value is used to then initialize string3 so we aren't modifying the returned value, simply using it to immediately initialize another variable.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Dec 2003
    Posts
    167
    My understanding is getting a little better but even if the value returned by the concatenation operator is not constant I don't see how it can be modified through string3. String3 itself is not constant so its value can change but if its value is a non const reference how would I change the value of that reference?

    With pointers I think its easier to understand. If only the value is constant then I should still be able to modify the variable to point to something else but aren't references supposed to be initialized when they're declared?

    Hmm i have a thought.

    a = b

    a = b is possible where a and b are references of the same type? But a = 4 is not since the assignment operator returns a constant reference? Is this how it works?
    Last edited by silk.odyssey; 05-12-2004 at 11:56 AM.
    silk.odyssey

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM