Search code examples
c++c++11

Why is my move assignment operator not working properly? It appears it's messing rest of couts in main as well


main.cpp

int main()
{
    Mystring larry3 = "larry3"; //will call no-arg ctor not move ctor.
    Mystring larry4 = larry3; // deep copy ctor is being called correctly
    std::cout <<"before" << endl;
    larry4 = "larry4"; // move assignment operator is being called correctly
    larry4 = Mystring{"larry4Again"}; // move assignment should be called -- but things
                                      // seems to be messed after this line
    larry4.display(); // this is also not working
    std::cout <<"after" << endl; // this is also not working

    return 0;
}

Mystring.cpp

//
// Created by kumarg on 17-11-2024.
//

#include "Mystring.h"
#include <iostream>
#include <cstring>

//no-arg ctror
Mystring::Mystring()
  :str{nullptr} {
  str = new char[1];
  *str = '\0'; //
  cout << "no-arg ctor" << endl;
}

//arg ctor
Mystring::Mystring(const char *s)
  :str{nullptr}{
  if(s == nullptr) { //if s points to nullptr then do same thing as no-arg ctor
    this->str = new char[1]; // this->str or str is same used in 3rd line from here
    *str = '\0';
  } else {
    str = new char[std::strlen(s) + 1];
    std::strcpy(this->str, s);
  }
  std::cout << "arg-ctor" <<endl;
}

//dtor
Mystring::~Mystring() {
  std::cout << "dtor called: " << str << endl;
  delete []str;
}

void Mystring::display() const {
  std::cout << this->str << endl;
}

//copy ctor - deep
Mystring::Mystring(const Mystring &source)
  :str{nullptr} {
  std::cout << "deep copy ctor" << endl;
  str = new char[std::strlen(source.str) +1];
  std::strcpy(this->str, source.str);
}

//move ctor
Mystring::Mystring(Mystring &&source)
  :str{source.str} {
  cout << "move ctor" << endl;
  source.str = nullptr;
}

//copy assignment operator implementation
Mystring &Mystring::operator=(const Mystring &rhs) {
  std::cout << "copy assignment operator" << endl;
  //check if both are same already
  if(this == &rhs)
    return *this;
  delete []str;
  str = new char[std::strlen(rhs.str) + 1];
  std::strcpy(this->str, rhs.str);
  return *this;
}

//move assignment operator
Mystring &Mystring::operator=(Mystring &&rhs) {
  std::cout << "move assignment operator" << endl;
  if(this == &rhs)
    return *this;
  delete []str;
  str = rhs.str;
  rhs.str = nullptr;
  return *this;
}

Mystring.h

//
// Created by kumarg on 16-11-2024.
//      

#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>

using namespace std;

class Mystring {
private:
    char *str;
public:
    Mystring(); // default ctor
    Mystring(const char *source); //parameter ctor
    ~Mystring();
    Mystring(const Mystring &source); //copy ctor
    Mystring(Mystring &&source) noexcept; //move ctor

    void display() const;
    Mystring operator-(); // unary - operator overloading
    bool operator==(Mystring &rhs); // assignment operator overloading
    bool operator!=(Mystring &rhs); // !- operator overloading


    bool operator<(Mystring &lhs);
    bool operator>(Mystring &lhs);
    Mystring operator+(Mystring &lhs);
    //assignment (=) operator overloading, remember this called when we do s2 = s1(both are initilaized already)
    // = operator can be overloaded in 2 ways, copy and move, if l-value thenp copy will be called, if r-value move will be called
    Mystring &operator=(const Mystring &rhs);
    // = op overloading by move
    Mystring &operator=(Mystring &&rhs);
    //Mystring &operator+=(Mystring &lhs);    
};

#endif //MYSTRING_H

My code executes as per expectation until this line in main:

larry4 = "larry4";

But, after this line:

larry4 = Mystring{"larry4Again"};

I was expecting an arg-ctor call and then a move assignment call. So I was expecting a cout from the arg-ctor and then a cout from the move operator assignment call. And then finally I was expecting cout from main.

None of that is happening, and I don't understand why.


Solution

  • The problem has nothing to do with your move assignment, but rather that in your Mystring destructor (Mystring::~Mystring) you do not check if str is null when attempting to print it in this line:

    std::cout << "dtor called: " << str << endl;
    

    As you can see here, operator<< overload for char* does not support a null pointer:

    If s is a null pointer, the behavior is undefined.

    You can fix it by changing the print statement to be:

    if (str) {
        std::cout << "dtor called: " << str << endl;
    }
    else {
        std::cout << "dtor called (null)" << endl;
    }
    

    Live demo