@@ -158,8 +158,14 @@ namespace Delegate
158158}
159159
160160// *4. Copy constructor: initialize an copy object with an existing object
161- // Generated if no copy/move constructor or destructor is declared
161+ // Generated if no copy/move constructor or ` destructor` is declared
162162// Performs memberwise (shallow) copy
163+ // * Copy constructor
164+ // Object obj1 = obj2
165+ // Object obj1(obj2)
166+ // Object obj1{obj2}
167+ // * Copy assigment
168+ // obj1 = obj1
163169namespace Copy
164170{
165171 class ICConstructor // C++ will create a public implicit copy constructor for us if we do not provide a one.
@@ -192,14 +198,27 @@ namespace Copy
192198 cout << " Called ICConstructor(int x, int y) : m_x{x}, m_y{y} \n " ;
193199 }
194200
201+ // Implicit Copy constructor if there is no desconstrutor
195202 // using `default` keyword
196203 // ECConstructor(const ECConstructor &ref) = default;
197204
205+ // Explicit Copy constructor (a=ECConstructor(b))
198206 ECConstructor (const ECConstructor &ref) : m_x{ref.m_x }, m_y{ref.m_x }
199207 {
200208 cout << " Called ECConstructor(const ECConstructor& ref) : m_x{ref.m_x}, m_y{ref.m_x} \n " ;
201209 }
202210
211+ // Copy assigment (a=b)
212+ ECConstructor &operator =(const ECConstructor &other)
213+ {
214+ if (this != &other)
215+ {
216+ this ->m_x = other.m_x ;
217+ this ->m_y = other.m_y ;
218+ }
219+ return *this ;
220+ }
221+
203222 void print () const
204223 {
205224 cout << " m_x = " << m_x << " , m_y = " << m_y << " \n " ;
@@ -246,12 +265,90 @@ namespace Copy
246265 }
247266}
248267
249- // *4. Copy constructor: initialize an copy object with an existing object
250- // Generated if no copy/move constructor or destructor is declared
251- // Performs memberwise (shallow) copy
252- namespace Copy
268+ // *4. Move Constructor:
269+ // Move constructor and move assignment transfer resource ownership
270+ // from one object to another. This is usually cheaper than copying.
271+ // A move constructor is implicitly generated only if no user-declared
272+ // copy constructor, move constructor, or destructor exists.
273+ namespace Move
253274{
275+ class Model // C++ will create a public implicit copy constructor for us if we do not provide a one.
276+ {
277+ private:
278+ int m_x;
279+ int m_y;
254280
281+ public:
282+ Model (int x, int y) : m_x{x}, m_y{y}
283+ {
284+ cout << " Call constructor \n " ;
285+ }
286+
287+ ~Model ()
288+ {
289+ cout << " Call destructor \n " ;
290+ }
291+
292+ Model (const Model &other) : Model(other.m_x, other.m_y)
293+ {
294+ cout << " Call copy constructor \n " ;
295+ }
296+
297+ Model &operator =(const Model &other)
298+ {
299+ cout << " Call copy assigment \n " ;
300+ if (this != &other)
301+ {
302+ this ->m_x = other.m_x ;
303+ this ->m_y = other.m_y ;
304+ }
305+ return *this ;
306+ }
307+
308+ Model (Model &&source) noexcept : m_x(source.m_x), m_y(source.m_y)
309+ {
310+ cout << " Call move constructor\n " ;
311+
312+ // reset source
313+ source.m_x = 0 ;
314+ source.m_y = 0 ;
315+ }
316+
317+ Model &operator =(Model &&source) noexcept
318+ {
319+ cout << " Call move assigment \n " ;
320+ if (this != &source)
321+ {
322+ this ->m_x = source.m_x ;
323+ this ->m_y = source.m_y ;
324+
325+ // reset source
326+ source.m_x = 0 ;
327+ source.m_y = 0 ;
328+ }
329+
330+ return *this ;
331+ }
332+
333+ void print () const
334+ {
335+ cout << " m_x = " << m_x << " , m_y = " << m_y << " \n " ;
336+ }
337+ };
338+
339+ void constructers ()
340+ {
341+ cout << " \n --- Move Constructor Examples ---\n " ;
342+ Model a (10 , 20 );
343+
344+ cout << " \n Case 1: Model b = std::move(a);\n " ;
345+ Model b = std::move (a); // move constructor
346+
347+ cout << " \n Case 2: Model c(5,6); c = std::move(b);\n " ;
348+ Model c (5 , 6 );
349+ c = std::move (b); // move assignment
350+ cout << " \n " ;
351+ }
255352}
256353
257354struct CConstructorsAutoRuner
@@ -262,6 +359,7 @@ struct CConstructorsAutoRuner
262359 Default::constructers ();
263360 Delegate::constructors ();
264361 Copy::constructors ();
362+ Move::constructers ();
265363 }
266364};
267365
0 commit comments