0

And I faced a concept called move constructor.

What I got from that concept we will use it to increase performance and reduce memory duplication by not copying the objects all over the place.

But I don't know how this work and I'm confused about this. What I understood it's like shallow copy constructor but it nulls out the source pointer of original object and copy the address.

Are objects passed by value? Is that why it needs to use copy constructor?

If I want to not copy object and work with the object itself, can I use & with object?

I know move constructors work with r-value which are temp parameters/objects.

class Move{
private: 
    int* data;
public:
    void set_data_value(int d) {*data = d;}
    int  get_data_value() {return *data;}
    //constructor
    Move (int d);
    //copy constructor
    Move(const Move &source);
    //Move constructor
    Move(Move &&source) noexcept
    //destructor
    ~Move();
};

Move::Move(int d) {
    data = new int;
    *data = d;
    }
    
//copy constructor
Move::Move(const Move &source)

//Move constructor
Move::Move(Move &&source) noexcept
    : data(source.data) {
        source.data = nullptr;
        }
        
Move::~Move {
    delete data;
    }

void display_move(Move s)   {           //this will use copy constructor to copy object to work with copied object
    cout << s.get_data_value << endl;
    }

void display_move(Move &s)  {           //can i use this to work with the actual object???
    cout << s.get_data_value << endl;
    }

int main() {
    vector<Move> vec;
    Move object1{10};               //l-value reference
    display_move(object1);          
    vec.push_back(Move{10});        //r-value reference so it will use move constructor
    vec.push_back(std::move(object1));  //casting l-value to r-value so it will use move constructor instead of copy constructor
Red
  • 26,798
  • 7
  • 36
  • 58
logan_92
  • 117
  • 9
  • 1
    Pass by value is the default in C++. With the exception of an array (because it [decays to a pointer](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay)) everything is passed by value unless you explicitly request pass by reference with `&`. As a result it can be really easy to accidentally copy. Move is the Five in [the Rule of Five](https://en.cppreference.com/w/cpp/language/rule_of_three) – user4581301 Jul 28 '20 at 00:42
  • Lots of typos in your code there. Please try to post code that at least compiles, see [mre]. – Paul Sanders Jul 28 '20 at 00:44
  • 1
    Note that move doesn't have to steal the guts of the source object. Sometimes it's just as cost effective for move to be a copy. In that case don't bother with a move constructor or move assignment operator. The compiler will figure it out. – user4581301 Jul 28 '20 at 00:45
  • 1
    Familiarize yourself with [RAII](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii) and [ownership](https://stackoverflow.com/questions/49024982/what-is-ownership-of-resources-or-pointers) and you'll get a better grasp of why C++ makes you deal with all this rigamarole. – user4581301 Jul 28 '20 at 00:48
  • @alterigel thank you for this link , i read part of it but i'm stuck on this Move::Move(Move &&source) noexcept : data(source.data) { source.data = nullptr; } how can i steal the data from temp object by using daa(source.data) => this line gets the address of the pointer in source/original temp object not the address of the object itself??? – logan_92 Jul 28 '20 at 00:57
  • 1
    here is the original proposal back in 2002: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1377.htm – Red.Wave Jul 28 '20 at 03:57

0 Answers0