operator overloading

This is a discussion on operator overloading within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by arian I have another question too. I am confused: I got 2 example functions which returns "*this". ...

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by arian View Post
    I have another question too.

    I am confused:
    I got 2 example functions which returns "*this".
    one of them is like:
    Code:
    class_name &  fun_name (class_name);
    the other one is:
    Code:
    class_name fun_name (class_name);
    if it is "return(*this)" in both of them , how come the return type is different?!
    Both of these return a "class_name", so that's ok. The first returns by reference, while the second returns by "copy".

  2. #17
    Registered User
    Join Date
    Nov 2003
    Posts
    177
    Quote Originally Posted by tabstop View Post
    Really? Do you not see the difference between "nothing" and "something"?
    No,

    I mean
    why --a calls [ loc operator--(); ]
    and
    why a-- calls [ loc operator--(int); ]

  3. #18
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    The function signatures for prefix and postfix are different, so if you can tell them apart, the compiler sure can. Is loc operator-- ( int ); completely the same as loc operator-- ( ); ? Now all you need to know is which does what operation.

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by arian View Post
    No,

    I mean
    why --a calls [ loc operator--(); ]
    and
    why a-- calls [ loc operator--(int); ]
    That's how the operators are defined. --a is defined to be operator--() and a-- is defined to be operator--(int).

  5. #20
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by arian View Post
    I have another question too.

    I am confused:
    I got 2 example functions which returns "*this".
    one of them is like:
    Code:
    class_name &  fun_name (class_name);
    the other one is:
    Code:
    class_name fun_name (class_name);
    if it is "return(*this)" in both of them , how come the return type is different?!
    I believe because it is a reference can be treated like the object itself, so the compiler wouldn't mind?
    Although if it was a pointer, it would very much mind
    You ended that sentence with a preposition...Bastard!

  6. #21
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by arian View Post
    I found this

    Code:
    loc loc::operator--(int)
    {
    	loc temp=*this;
                    lenght--;
    	width--;
    	return (temp);
    }
    will solve the problem.

    "this" points to the current obj.
    Before changing anything, copy it into temp.
    by
    lenght--;
    width--;
    the object will be changed but the previous value is returned by return(temp).
    I don't get it.
    You do not update the object however?
    so how will it get updated when you return temp?
    unless you do
    obj1=--obj1 :S
    You ended that sentence with a preposition...Bastard!

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Eman View Post
    I don't get it.
    You do not update the object however?
    That's what length-- and width-- do: they modify the object.

  8. #23
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by tabstop View Post
    That's what length-- and width-- do: they modify the object.
    oh right
    Last edited by Eman; 12-22-2010 at 03:17 PM.
    You ended that sentence with a preposition...Bastard!

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21