Demonstrate the principle of moving a temporary rather than creating a copy - C++ Class

C++ examples for Class:object

Description

Demonstrate the principle of moving a temporary rather than creating a copy

Demo Code

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

class Person{/*from w  w  w .j  a va 2 s  .co  m*/
  public:
    Person(const char *pN){
        pName = new string(pN);
        cout << "Constructing " << *pName << endl;
    }
    Person(Person& p){
        cout << "Copying " << *p.pName << endl;
        pName = new string("Copy of ");
        *pName += *p.pName;
    }
    Person(Person&& p){
        cout << "Moving " << *p.pName << endl;
        pName = p.pName;
        p.pName = nullptr;
    }
    ~Person(){
        if (pName){
            cout << "Destructing " << *pName << endl;
            delete pName;
        }else{
            cout << "Destructing null object" << endl;
        }
    }

 protected:
    string* pName;
};

Person fn2(Person p)
{
    cout << "Entering fn2" << endl;
    return p;
}

Person fn1(const char* pName)
{
    cout << "Entering fn1" << endl;
    return fn2(*new Person(pName));
}

int main(int argcs, char* pArgs[])
{
    Person s(fn1("Tom"));

    return 0;
}

Result


Related Tutorials