c++copy-assignmentmove-assignment-operator

c++ copy assignment and move assignment are not being called


I am trying to implement copy and move assignments, but I don't understand how should I use them. I have read the following topic
When did copy assignment operator called?
But it did not work for me.

Class:

class Directory{

    string name;
public:
        Directory(string name):name(name) {

        }
        ~Directory() {
            cout << "Deleting was called" <<endl;

            }

        Directory& operator=(Directory& other){
            cout << "cp assigment" <<endl;
            return *this;
        }
        Directory& operator=(Directory&& other){
            cout << "move assigment" <<endl;
            return *this;
        }
};

Main

int main()
{

    Directory* dir = new Directory("alex");
    Directory* dir2;
    dir = dir2;

    cout<<"done"<<endl;
};

I would like to know when the copy assignment and move assignment are called. Thanks in advance.


Solution

  • I my first comment, I recommended to remove all *s and the new.

    Hence the main function becomes:

    int main()
    {
      Directory dir = Directory("alex");
      Directory dir2;
      dir2 = dir; // <-- fixed, original was: dir = dir2;
    
      cout<<"done"<<endl;
      return 0; // <-- fixed, return is strictly recommended for every non-void function
    }
    

    Compiling...

    Error: Something is wrong in Directory dir = Directory("alex"); (usage of deleted copy constructor).

    The copy-constructor is used to initialize dir with the temporary instance created by Directory("alex").

    This is easy to change:

    int main()
    {
      Directory dir("alex"); // <-- fixed: direct construction
      Directory dir2;
      dir2 = dir;
    
      cout<<"done"<<endl;
      return 0;
    }
    

    Compiling...

    Error: Something is wrong in Directory dir2;.

    A yepp. You defined constructor Directory(string name);. This suppresses the auto-creation of the default constructor which would be needed here.

    We either could add the default constructor to class Directory:

      Directory() = default;
    

    or we can improve the existing non-default constructor so that it can be used as default constructor as well:

      Directory(string name = string()): name(name) { }
    

    The whole source:

    #include <iostream>
    #include <string>
    using namespace std;
    
    class Directory{
    
        string name;
    public:
            Directory(string name = string()):name(name) {
    
            }
            ~Directory() {
                cout << "Deleting was called" <<endl;
    
                }
    
            Directory& operator=(Directory& other){
                cout << "cp assigment" <<endl;
                return *this;
            }
            Directory& operator=(Directory&& other){
                cout << "move assigment" <<endl;
                return *this;
            }
    };
    
    int main() {
        //Directory dir = Directory("alex");
        Directory dir("alex");
        Directory dir2;
        dir2 = dir;
    
        cout<<"done"<<endl;
        // your code goes here
        return 0;
    }
    

    Now, it compiles and works.

    Output:

    cp assigment
    done
    Deleting was called
    Deleting was called
    

    You can see it live on ideone.