using = to trigger the call of copy constructor
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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++){
|
||||
|
||||
Reference in New Issue
Block a user