Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
MovConstrAssign2
//g++ 7.4.0 ////////////////////////////////////////////////////////////////////////////// //MovConstrAssign3: understanding Move constructor and assignment //this code is created by Rezaul Hoque on June 24,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=new std::string; *m=z;} M(M& c){m=c.m; std::cout<<"Move construction failed. Copy constructor in work\n";} M(M&& ot ): m(nullptr){ std::cout<<"Move constructor in work\n"; 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;} }; 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; N n2=N(n1); std::cout<<n2.getM(); return 0; }
run
|
edit
|
history
|
help
0
KhadijahAlshehhi
Identifying polimorphic types without using RTTI or type mappings
MovConstrAssign
std_minmax_unexpected_behaviour.cpp
pangram
Defining Class Members
read_write_lock_acc
Collatz Conjecture
ADVENTURE CODE CSCI 40
mine