overloaded operators

This is a discussion on overloaded operators within the C++ Programming forums, part of the General Programming Boards category; what is difference in these two codes as they are giving different outputs..?? Code: circle operator=(circle c) { circle t; ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    8

    overloaded operators

    what is difference in these two codes as they are giving different outputs..??
    Code:
    circle operator=(circle c)
    {
    circle t;
    t.radius=c.radius;
    t.x=c.x;
    t.y=c.y;
    return t;
    }
    Code:
    circle operator=(circle c)
    {
    radius=c.radius;
    x=c.x;
    y=c.y;
    return circle(radius,x,y);
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,252
    I am assuming both of those function definitions are within the definition for class circle. Otherwise, both samples will give a compilation error.

    Given an expression "lhs = rhs;" (where lhs and rhs are both of type circle) the first version does not change lhs. The second one does.

    Conventionally, assignment operators accept an argument that is a const reference, and return a reference. Rather than (as in both your cases) accepting an argument by value and returning a value.

    Conceptually, you are missing the fact that assignment operators are member functions, with an implied "this" pointer.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using overloaded operators
    By Niels_M in forum C++ Programming
    Replies: 2
    Last Post: 11-09-2010, 06:16 AM
  2. Overloaded operators
    By Kayoss in forum C++ Programming
    Replies: 6
    Last Post: 04-17-2006, 02:23 PM
  3. overloaded operators
    By jccharl in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2004, 05:14 PM
  4. overloaded operators
    By ivandn in forum C++ Programming
    Replies: 2
    Last Post: 12-20-2001, 02:52 PM
  5. help with overloaded operators
    By doleman19 in forum C++ Programming
    Replies: 23
    Last Post: 10-23-2001, 02:40 AM

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