/* * Code illustrant les differents constructeurs, fourni aux pages 644-645 du * chapitre 18 du livre "Programming, Principles and practises using C++" * de Bjarne Stroustrup. */ #include #include using namespace std; struct X{ int val; void out (const string& s, int nv) { cerr << this << "->" << s << ": " << val << "(" << nv << ")\n"; } X(){out("X()", 0); val=0;} //default constructor X(int v){val = v; out ("X(int)", v);} X(const X&x) {val = x.val; out("X(X&)", x.val);} //copy constructor X& operator=(const X&a) //copy assignment {out("X::operator=()", a.val); val=a.val; return *this;} ~X() {out("~X()", 0);} }; X glob(2); X copy(X a){return a;} X copy2(X a){X aa = a; return a;} X& ref_to(X& a) {return a;} X* make(int i){X a(i); return new X(a);} struct XX{X a; X b;}; int main(){ X loc(4); X loc2{loc}; loc = X(5); loc2 = copy(loc); loc2 = copy2(loc); X loc3{6}; X& r = ref_to(loc); delete make(7); delete make(8); XX loc4; X* p = new X{9}; delete p; X *pp = new X[5]; delete[] pp; }