1

I have made a simple application with move copy constructor and move copy assignment operator and on each of them I have made a cout statement just to tell me, which are being executed. But during execution, I did not see any statement from the move copy operators but only from default one which the compiler already provides. Here is my code:

#include <iostream>
#include <string>
using namespace std;

class Names{
private:
    string* name;
public:
    Names(string place_name){
        cout << "Overloaded constructor" << endl;
        name = new string;
        *name = place_name;
    }
    //copy constructor
    Names(Names& cpy){
        cout << "Copy constructor" << endl;
        name = new string;
        *name = *cpy.name;
    }
    //assignment 
    Names& operator =(const Names& cpy){
        cout << "Assignment operator" << endl;
        name = new string;
        *name = *cpy.name;
        return *this;
    }
    //move constructor
    Names(Names&& cpym){
        cout << "Copy move constructor" << endl;
        name = cpym.name;
        cpym.name = NULL;
    }
    //move assignment operator
    Names& operator=(Names&& cpym){
        cout << "Copy assignment operator" << endl;
        delete name;
        name = cpym.name;
        cpym.name = NULL;
        return *this;
    }
    //destructor
    ~Names(){
        cout << "Deallocating memory" << endl;
        delete [] name;
    }
};

int main(){
    Names nme("Bob");
    Names copin("something");
    copin = nme;
    system("pause");
    return 0;
} 

and here is the output

Output screen

so the main question are

1) Why isn't the cout statement being show for the move constructor?
2) Is my declaration for move constructor correct

and Thanks you.

R. Barzell
  • 666
  • 5
  • 24
user3264250
  • 77
  • 1
  • 6
  • You should get in the habit of using a constructor initializer list and `nullptr`. Otherwise, I'm not sure why you're expecting the move constructor to be called at all, and the compiler can still generate move versions for you. – chris Mar 06 '14 at 01:44
  • Like how exactly @chris because I am new to c++ and I would be even better if you can show me the coded version – user3264250 Mar 06 '14 at 01:48
  • I'd suggest a [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) then. Plus there's tons of information about each thing I mentioned on the Internet. – chris Mar 06 '14 at 01:52
  • @chris I got this information from 'Sams teaches yourself c++ in 21 days' and where can I find your tutorials? – user3264250 Mar 06 '14 at 01:54
  • I wasn't referring to any specific tutorial. They're all over the place and accessible through Google. The only specific thing I referred to was that list of books (and note that this particular book is not overly well-received). – chris Mar 06 '14 at 02:00
  • @chris well anyway, what could be the solution to this. Is there anything I'd have to change. – user3264250 Mar 06 '14 at 02:13

1 Answers1

1

You need to use std::move for the move constructor and move assignment operator to work. See my main function below:

Names A(Names name) {
    return name;
}
int main(){
    Names nme("Bob");
    Names copin("something");
    cout << "before std::move(nme);" << endl;
    copin = std::move(nme);
    cout << "before std::move(GetName());" << endl;
    Names tom = std::move(nme);
    cout << "before A(Names(\"dick\");" << endl;
    // move constructor is also called when using temp rvalue
    Names dick = A(Names("dick"));
    system("pause");
    return 0;
}

Output:

Overloaded constructor
Overloaded constructor
before std::move(nme);
Copy assignment operator
before std::move(GetName());
Copy move constructor
before A(Names("dick");
Overloaded constructor
Copy move constructor
Copy constructor
Deallocating memory
Deallocating memory
Press any key to continue . . .

One more issue, your destructor should not delete [] name, just delete name;

uncletall
  • 6,609
  • 1
  • 27
  • 52