c++0x characteristics in VC10 Part 2 (2): reference to right value

Source: Internet
Author: User
Tags constructor include

This is the second page of Part 2.

Move semantics: Moving from Lvalue

Now, if you like to use a copy assignment function to implement your copy constructor, you may also try to implement the move constructor with the move copy assignment function. This is OK, but you have to be careful. The following is an error implementation:

C:\Temp>type unified_wrong.cpp
#include <stddef.h>
#include <iostream>
#include <ostream>
using namespace std;

class remote_integer {

Public

Remote_integer () {
cout << "Default constructor." << Endl;

m_p = NULL;
}

Explicit Remote_integer (const int n) {
cout << "unary constructor." << Endl;

m_p = new int (n);
}

Remote_integer (const remote_integer& Other) {
cout << "Copy constructor." << Endl;

m_p = NULL;
*this = other;
}

#ifdef movable
Remote_integer (remote_integer&& other) {
cout << "Move constructor." << Endl;

m_p = NULL;
*this = other; Wrong
}
#endif//#ifdef movable

remote_integer& operator= (const remote_integer& Other) {
cout << "Copy assignment operator." << Endl;

if (this!= &other) {
Delete m_p;

if (other.m_p) {
m_p = new int (*other.m_p);
} else {
m_p = NULL;
}
}

return *this;
}

#ifdef movable
remote_integer& operator= (remote_integer&& other) {
cout << "Move assignment OPERATOR." << Endl;

if (this!= &other) {
Delete m_p;

m_p = other.m_p;
other.m_p = NULL;
}

return *this;
}
#endif//#ifdef movable

~remote_integer () {
cout << "destructor." << Endl;

Delete m_p;
}

int get () const {
Return m_p? *m_p:0;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.