Notice
Recent Posts
Recent Comments
Link
헤더파일
c++ Brace Init 본문
class Point
{
int x, y;
public:
explicit Point(int a, int b) : x(a), y(b) {}
};
void foo(Point p) {}
int main()
{
foo({ 1,1 });
// Point p1(1, 1);
// Point p2{ 1, 1 }; // direct initialize
// Point p3 = { 1, 1 };// copy initialize. error
}
p3 초기화는 explicit가 없다면 복사 생성자가 실행되어 초기화 됩니다. 하지만 explicit를 넣어준다면 불필요한 복사생성을 막을 수 있습니다.
'C++' 카테고리의 다른 글
C++ begin/end (1) | 2020.03.26 |
---|---|
C++ static_assert (0) | 2020.03.26 |
C++ Explicit Conversion Operator (0) | 2020.03.25 |
c++ 변환 연산자, 변환 생성자 (0) | 2020.03.23 |
C++17 if 초기화, if constexpr, 구조체 바인딩 (1) | 2020.03.23 |