Search code examples
c++operator-overloadingcopy-constructorassignment-operatorcopy-and-swap

Utilizing Copy constructor for =overloading


I have a class with two vectors: an int and an Str. Now I want to define a copy constructor so that the order of elements is reversed; e.g. if a=(1,Hello),(2,World) and I write auto b=a; I get b=(2,world),(1,hello). Which is working perfectly. The issue I am having is overloading the = operator so that the copy constructor is utilized. Here is my Class plus copy constructor:

class grade
{
  private:
    static int copies;
    std::vector<int> i;
    std::vector<std::string> s;

  public:
    grade() {copies++;};
    grade (int , std::string );
    void printval();
    void adder(int , std::string );
    int getcount();

    grade(grade & obj)
    {
        std::vector<std::string>::reverse_iterator strIt = obj.s.rbegin();
        for (std::vector<int>::reverse_iterator numIt=obj.i.rbegin();
             numIt!=obj.i.rend(); ++numIt)
        {
            this->adder(*numIt, *strIt);
            strIt++;
        }

    }
    grade operator =(grade );
};

When I overload the = operator the constructor is called and the trouble is that the valur is not passed to the LHS variable. Here is the overloaded =.

grade grade::operator=(grade cpy)
{
    grade newer = cpy; //Calls the copy constructor as expected
    return cpy;        //No error but blank value is returned to the LHS variable.
}

My main function is :

int main()
{
    grade c2(1,"Hello");
    grade c1;

    c2.adder(4,"World");
    c2.printval();
    std::cout<<std::endl;
    grade c3 = c2;
    c3.printval();
    std::cout<<std::endl;
    c1 = c2;
    std::cout<<std::endl;
    c1.printval();

    return 0;
}

Why does c1 remain blank??


Solution

  • You asked: Why does c1 remain blank??

    The answer is that the assignment operator does not modify the object itself. Here's your code:

    grade grade::operator=(grade cpy)
    {
      grade newer=cpy; //Calls the copy constructor as expected
      return cpy; //No error but blank value is returned to the LHS variable.
    }
    

    The method returns a copy of the input parameter cpy but does not modify the current instance. When this method is executing, *this is the "LHS variable", and it's not being modified at all. Within this method (operator overload, you need to actually modify the member variables of the current instance!