Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
MovConstrAssign
//g++ 7.4.0 ////////////////////////////////////////////////////////////////////////////// //MovConstrAssign: understanding Move constructor and assignment //this code is created by Rezaul Hoque on June 23,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){ m=new std::string; *m=z;} M(M&& ot ): m(nullptr){ m=ot.m; ot.m=nullptr; } M& operator=(M&& ot) { if (this != &ot) { delete m; m=ot.m; ot.m=nullptr; } std::cout<<"Move assignment is in work\n"; return * this; } std::string getM(){ return * m;} }; std::string getStr(){ std::string s="Hi\n"; return s; } class N :public M{ public: std::string* n; }; class O: N{ public: ~O(){}//destructor blocks move constructor and assignment }; int main() { C a("hi\n"); std::cout<<a.getS(); //Move constructor M z=getStr(); std::cout<<z.getM(); //Move assignment operator M g("there\n"); g=getStr(); std::cout<<g.getM(); return 0; }
run
|
edit
|
history
|
help
0
offsetof
Updated Linked Lists - 5/10/2017 V3.0
always_late
Coin changes
offsetof
semiprog.cpp
¡Arriba, Papalotes, Arriba! v2
პირამიდის ფართობი~ფინალური
Ballin primality test
Bank System