From 836a63f8f1bb9dabb8c42f2efb8277264c25dd4a Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Thu, 14 Sep 2023 18:15:16 -0400 Subject: [PATCH] using = to trigger the call of copy constructor --- lectures/05_classes_II/README.md | 2 +- leetcode/p1929_concatenationofarray.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lectures/05_classes_II/README.md b/lectures/05_classes_II/README.md index 1064738..f14b3cf 100644 --- a/lectures/05_classes_II/README.md +++ b/lectures/05_classes_II/README.md @@ -47,7 +47,7 @@ will sort Date objects into chronological order. - A copy constructor is a constructor which is used to create a new object as a copy of an existing object of the same class. - Copy constructors are automatically generated by the compiler if you do not provide one explicitly. However, if your class uses dynamic memory (which will be covered in next lecture), and you want a copy constructor, then you must write your own copy constructor. -- Copy constructors get called when you create a new object by copying an existing object using the assignment operator (=), or when you pass an object by value (NOT by a reference) to a function. +- Copy constructors get called when you create a new object by copying an existing object using the assignment operator (=), or when you pass an object by value to a function. - Still use the *Date* class as an example, if you have defined your own copy constructor whose prototype is like: ```cpp diff --git a/leetcode/p1929_concatenationofarray.cpp b/leetcode/p1929_concatenationofarray.cpp index 601ca93..9c4a347 100644 --- a/leetcode/p1929_concatenationofarray.cpp +++ b/leetcode/p1929_concatenationofarray.cpp @@ -25,7 +25,7 @@ class Solution { public: vector getConcatenation(vector& nums) { // use the copy constructor - vector ans(nums); + vector ans = nums; int size = nums.size(); // copy the second half for(int i=0;i