일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- kaggle
- 시바견
- Regular Expression
- Python Implementation
- Protocol
- 운영체제
- iterator
- Decorator
- attribute
- t1
- 315. Count of Smaller Numbers After Self
- 43. Multiply Strings
- Python Code
- Class
- 파이썬
- LeetCode
- Convert Sorted List to Binary Search Tree
- 109. Convert Sorted List to Binary Search Tree
- concurrency
- shiba
- 프로그래머스
- Python
- data science
- 밴픽
- 컴퓨터의 구조
- 30. Substring with Concatenation of All Words
- DWG
- 715. Range Module
- Substring with Concatenation of All Words
- Generator
Archives
- Today
- Total
Scribbling
[C++] 전문가를 위한 C++ - Chapter 8 본문
1. Default Constructor
In C++, when making an array, we cannot explicitly call a constructor other than the default one.
2. Converting Constructor
C++ compiler may implicitly convert a value to an object. (In this case, integer to Cell object)
class Cell {
public:
int num = 0;
Cell() = default;
Cell(int num) {
this->num = num;
}
};
int main() {
Cell cell1{ 4 };
cell1 = 5;
cout << cell1.num << endl;
}
To prevent such, we can use "explicit" keyword.
class Cell {
public:
int num = 0;
Cell() = default;
explicit Cell(int num) {
this->num = num;
}
};
int main() {
Cell cell1{ 4 };
cell1 = 5;
cout << cell1.num << endl;
}
3. Copy Constructor & Assignment Operator
The copy constructor is only used when initiating an object.
In the below, cell 2 and cell 3 call the copy constructor.
int main() {
Cell cell1{ 5 };
Cell cell2{ cell1 };
Cell cell3 = cell1;
}
*** When a function returns an object, it calls the copy constructor.
*** Constructor Initializer also calls the copy constructor.
'Computer Science > C++' 카테고리의 다른 글
[C++] lower_bound (0) | 2024.02.06 |
---|---|
[C++] Deque<int> (0) | 2024.02.06 |
[C++] find, deque, bfs, structured binding (0) | 2023.09.30 |
[C++] Smart pointers (0) | 2023.09.30 |
[C++] 전문가를 위한 C++ - Chapter 2 (1) | 2023.09.21 |