Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
MovConstrAssign4
//g++ 7.4.0 //////////////////////////////////////////////////////////////////////////// //MovConstrAssign4: understanding Move constructor and assignment using move semantics //this code is created by Rezaul Hoque on July 01,2022; //contact:jewelmrh@yahoo.com;Dhaka,Bangladesh;https://rezaulhoque.wordpress.com,https://hoquestake.blogspot.com //note: codes shared by Rezaul Hoque on rextester are not for sale; they are created and shared to facilitate the algorithm learning process; many like Hoque use this platform to practice programming ;Rezaul hopes his contribution helps others to fine tune their learning; ////////////////////////////////////////////////////////////////////////////// #include <iostream> #include <string> class C{ std::string s; public: C(std::string a){s=a;} C(C& c){std::cout<<"Copy constructor in work\n"; s=c.s;} ~C(){} std::string getS(){return s;} }; class M{ std::string m; public: M(std::string z="Hi\n"){ m=z;} M(const M& c){m=c.m; std::cout<<"Move construction failed. Copy constructor in work\n";} M(M&& ot ){ std::cout<<"Move constructor in work\n"; m=std::move(ot.m); } M& operator=(M&& ot) { if (this != &ot) { m=std::move(ot.m); } std::cout<<"Move assignment is in work\n"; return * this; } std::string getM(){ return m;} }; M getStr(M a){ return a; } class N :public M{ public: std::string n; }; class O: public N{ public: ~O(){}//destructor blocks implicit move constructor and assignment }; class P : public N{ public: std::string p; }; int main() { C a("hi\n"); std::cout<<a.getS(); //Move constructor M z=getStr(M()); std::cout<<z.getM(); std::cout<<"After move:\n"; std::cout<<z.getM(); //Move assignment operator M g("there\n"); std::cout<<"Before move constructor: \n"; std::cout<<g.getM(); std::cout<<"After move constructor: \n"; g=getStr(M()); std::cout<<g.getM(); N n1; std::cout<<"Before move semantics: \n"; std::cout<<"n1.getM() :"; std::cout<<n1.getM(); N n2=std::move(n1); std::cout<<"After move constructor: \n"; std::cout<<"n1.getM() :"; std::cout<<n1.getM(); std::cout<<"\nn2.getM() :"; std::cout<<n2.getM(); O o1; O o2=std::move(o1); return 0; }
run
|
edit
|
history
|
help
0
1068 - Investigation
Вывод элементов массива
BinomialPoisson
UtilityPair
Using c++11 range-base for loop
typedef
GCC bug #79511
UB mutex lock
123
D three integers