//demo.h
#ifndef _DEMO_H_
#define _DEMO_H_
class Demo {
public:
Demo();
Demo(Demo const & d);
Demo(Demo && d);
Demo & operator=(Demo && d);
Demo & operator=(const Demo & d);
~Demo();
};
#endif
//Demo.cpp
#include<iostream>
#include "Demo.h"
using namespace std;
Demo::Demo() {
cout << this << " - Demo::Demo() " << endl;
}
Demo::Demo(Demo const & d) {
cout << this << " - Demo::Demo(Demo const & )" << endl;
}
Demo::Demo(Demo && d) {
cout << this << " - Demo::Demo(Demo &&)" <<endl;
}
Demo & Demo::operator=(Demo && d) {
cout << this << " - Demo::operator =(Demo && ) " << endl;
return *this;
}
Demo & Demo::operator=(const Demo & d) {
cout << this << " - Demo::operator=(const Demo & d) " <<endl;
return *this;
}
Demo::~Demo() {
cout << this << " - Demo::~Demo() " <<endl;
}
//return_demo.cpp
#include "Demo.h"
#include <utility>
#include <iostream>
using namespace std;
Demo fun(){ Demo s; return s; }
Demo gun(){ return move(Demo{}); }
Demo && rrfun(){ Demo s; return move(s); }
int main(){
cout << " ------ Demo fun(){ Demo s; return s; } " << endl;
Demo x=fun();
cout << " addres of return value is " << &x << endl;
cout << "\n move -------------" <<endl ;
Demo xm=move(fun());
cout << " addres of return value is " << &xm << endl;
cout << " \n------ Demo gun(){ return move(Demo{}); } " << endl;
Demo y=gun();
cout << " addres of return value is " << &y << endl;
cout << "\n move -------------" <<endl ;
Demo ym=move(gun());
cout << " addres of return value is " << &ym << endl;
cout << " \n------ Demo && rrfun(){ Demo s; return move(s); } " << endl;
Demo z=rrfun();
cout << " addres of return value is " << &z << endl;
cout << "\n move -------------" <<endl ;
Demo zm=move(rrfun());
cout << " addres of return value is " << &zm << endl;
cout << " ------------- main end -------------- " << endl;
return 0;
}
//out put
------ Demo fun(){ Demo s; return s; }
0x7ffc6f560b78 - Demo::Demo()
addres of return value is 0x7ffc6f560b78
move -------------
0x7ffc6f560b7a - Demo::Demo()
0x7ffc6f560b79 - Demo::Demo(Demo &&)
0x7ffc6f560b7a - Demo::~Demo()
addres of return value is 0x7ffc6f560b79
------ Demo gun(){ return move(Demo{}); }
0x7ffc6f560b4f - Demo::Demo()
0x7ffc6f560b7b - Demo::Demo(Demo &&)
0x7ffc6f560b4f - Demo::~Demo()
addres of return value is 0x7ffc6f560b7b
move -------------
0x7ffc6f560b4f - Demo::Demo()
0x7ffc6f560b7d - Demo::Demo(Demo &&)
0x7ffc6f560b4f - Demo::~Demo()
0x7ffc6f560b7c - Demo::Demo(Demo &&)
0x7ffc6f560b7d - Demo::~Demo()
addres of return value is 0x7ffc6f560b7c
------ Demo && rrfun(){ Demo s; return move(s); }
0x7ffc6f560b4f - Demo::Demo()
0x7ffc6f560b4f - Demo::~Demo()
0x7ffc6f560b7e - Demo::Demo(Demo &&)
addres of return value is 0x7ffc6f560b7e
move -------------
0x7ffc6f560b4f - Demo::Demo()
0x7ffc6f560b4f - Demo::~Demo()
0x7ffc6f560b7f - Demo::Demo(Demo &&)
addres of return value is 0x7ffc6f560b7f
------------- main end --------------
0x7ffc6f560b7f - Demo::~Demo()
0x7ffc6f560b7e - Demo::~Demo()
0x7ffc6f560b7c - Demo::~Demo()
0x7ffc6f560b7b - Demo::~Demo()
0x7ffc6f560b79 - Demo::~Demo()
0x7ffc6f560b78 - Demo::~Demo()
we are reusing the same object which is created inb fun() and no constructor is called while returning object ( ellison rule )
Demo fun(){ Demo s; return s; }
we are calling move constructor while returning object and a new object is created in below functions
Demo gun(){ return move(Demo{}); }
Demo && rrfun(){ Demo s; return move(s); }
from the above demo program and output can we assume that object return by value is better then returning the by move // rvale reference ??
Aucun commentaire:
Enregistrer un commentaire