assignment operator overloading in c++

前端 未结 6 1927
醉梦人生
醉梦人生 2021-02-01 15:07

I have used the following code for assignment operator overloading:

SimpleCircle SimpleCircle::operator=(const SimpleCircle & rhs)
{
     if(this == &rhs         


        
相关标签:
6条回答
  • 2021-02-01 15:22
    #include<iostream>
    
    using namespace std;
    
    class employee
    {
        int idnum;
        double salary;
        public:
            employee(){}
    
            employee(int a,int b)
            {
                idnum=a;
                salary=b;
            }
    
            void dis()
            {
                cout<<"1st emp:"<<endl<<"idnum="<<idnum<<endl<<"salary="<<salary<<endl<<endl;
            }
    
            void operator=(employee &emp)
            {
                idnum=emp.idnum;
                salary=emp.salary;
            }
    
            void show()
            {
                cout<<"2nd emp:"<<endl<<"idnum="<<idnum<<endl<<"salary="<<salary<<endl;
            }
    };
    
    main()
    {
        int a;
        double b;
    
        cout<<"enter id num and salary"<<endl;
        cin>>a>>b;
        employee e1(a,b);
        e1.dis();
        employee e2;
        e2=e1;
        e2.show();  
    }
    
    0 讨论(0)
  • 2021-02-01 15:23

    There are no problems with the second version of the assignment operator. In fact, that is the standard way for an assignment operator.

    Edit: Note that I am referring to the return type of the assignment operator, not to the implementation itself. As has been pointed out in comments, the implementation itself is another issue. See here.

    0 讨论(0)
  • 2021-02-01 15:31

    it's right way to use operator overloading now you get your object by reference avoiding value copying.

    0 讨论(0)
  • 2021-02-01 15:33

    Under the circumstances, you're almost certainly better off skipping the check for self-assignment -- when you're only assigning one member that seems to be a simple type (probably a double), it's generally faster to do that assignment than avoid it, so you'd end up with:

    SimpleCircle & SimpleCircle::operator=(const SimpleCircle & rhs)
    {
        itsRadius = rhs.getRadius(); // or just `itsRadius = rhs.itsRadius;`
        return *this;
    }
    

    I realize that many older and/or lower quality books advise checking for self assignment. At least in my experience, however, it's sufficiently rare that you're better off without it (and if the operator depends on it for correctness, it's almost certainly not exception safe).

    As an aside, I'd note that to define a circle, you generally need a center and a radius, and when you copy or assign, you want to copy/assign both.

    0 讨论(0)
  • 2021-02-01 15:41

    The second is pretty standard. You often prefer to return a reference from an assignment operator so that statements like a = b = c; resolve as expected. I can't think of any cases where I would want to return a copy from assignment.

    One thing to note is that if you aren't needing a deep copy it's sometimes considered best to use the implicit copy constructor and assignment operator generated by the compiler than roll your own. Really up to you though ...

    Edit:

    Here's some basic calls:

    SimpleCircle x; // default constructor
    SimpleCircle y(x); // copy constructor
    x = y; // assignment operator
    

    Now say we had the first version of your assignment operator:

    SimpleCircle SimpleCircle::operator=(const SimpleCircle & rhs)
    {
         if(this == &rhs)
            return *this; // calls copy constructor SimpleCircle(*this)
         itsRadius = rhs.getRadius(); // copy member
         return *this; // calls copy constructor
    }
    

    It calls the copy constructor and passes a reference to this in order to construct the copy to be returned. Now in the second example we avoid the copy by just returning a reference to this

    SimpleCircle & SimpleCircle::operator=(const SimpleCircle & rhs)
    {
        if(this == &rhs)
           return *this; // return reference to this (no copy)
        itsRadius = rhs.getRadius(); // copy member
        return *this; // return reference to this (no copy)
    }
    
    0 讨论(0)
  • 2021-02-01 15:41

    this might be helpful:

    // Operator overloading in C++
    //assignment operator overloading
    #include<iostream>
    using namespace std;
    
    class Employee
    {
    private:
    int idNum;
    double salary;
    public:
    Employee ( ) {
        idNum = 0, salary = 0.0;
    }
    
    void setValues (int a, int b);
    void operator= (Employee &emp );
    
    };
    
    void Employee::setValues ( int idN , int sal )
    {
    
    salary = sal; idNum = idN;
    
    }
    
    void Employee::operator = (Employee &emp)  // Assignment operator overloading function
    {
    salary = emp.salary;
    }
    
    int main ( )
    {
    
    Employee emp1;
    emp1.setValues(10,33);
    Employee emp2;
    emp2 = emp1; // emp2 is calling object using assignment operator
    
    }
    
    0 讨论(0)
提交回复
热议问题