using = to trigger the call of copy constructor

This commit is contained in:
Jidong Xiao
2023-09-14 18:15:16 -04:00
parent 32f60bab51
commit 836a63f8f1
2 changed files with 2 additions and 2 deletions

View File

@@ -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

View File

@@ -25,7 +25,7 @@ class Solution {
public:
vector<int> getConcatenation(vector<int>& nums) {
// use the copy constructor
vector<int> ans(nums);
vector<int> ans = nums;
int size = nums.size();
// copy the second half
for(int i=0;i<size;i++){