프로그래밍
![[C++] const member function](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbKX4vb%2FbtqzYCY6XnT%2Fy8p7F7FyuH5WTxwn979Bwk%2Fimg.png)
[C++] const member function
1. 상수 멤버 함수 (const member function) #include class Point { int x, y; public: Point(int a = 0, int b = 0) : x(a), y(b) {} void set(int a, int b) { x = a; y = b; } void print() const { x = 10; std::cout 멤버 데이터의 값을 읽을 수는 있지만 변경할 수는 없다. ③ 상수 멤버 함수를 사용하는 이유 - 중요! => 코드 작성시 안정성 => 상수 객체는 상수 멤버 함수만 호출할 수 있다. 2. 상수 멤버 함수가 필요한 이유 #include class Point { public: int x, y; Point(int a = 0, int b = 0) : x(a)..
![[C++] static member function](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FdALAKf%2FbtqzOWpyDb5%2FUOX2ZgQ2QPc3kIRlVpeJH0%2Fimg.png)
[C++] static member function
1. 정적 멤버 함수 (static member function) class Car { int speed; static int cnt; public: Car() { ++cnt; } ~Car() { --cnt; } int getCount() { return cnt; } }; int Car::cnt = 0; int main() { Car c1, c2, c3; c1.getCount(); } static member data는 객체를 생성하지 않아도 메모리에 올라간다. 위의 코드에서는 getCount() 함수를 통해 객체의 개수를 파악하고 있다. 그런데, 객체를 생성하지 않은 상태에서 개수를 파악하고 싶을 때 getCount()를 어떻게 호출할까? static int getCount() { return cnt;..
![[C++] static member data](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FblbFUH%2FbtqzNBrmgit%2FBiJ7sDCaGy9PeBqdHuXNKK%2Fimg.png)
[C++] static member data
1. 정적 멤버 데이터 (static member data) #include class Car { int speed; int color; public: Car() {} ~Car() {} }; int main() { Car c1; Car c2; Car c3; } ① 객체의 생성과 소멸 => 모든 객체는 생성 될 때 생성자를 호출하고 => 모든 객체는 파괴 될 때 소멸자를 호출한다. ② 객체가 몇 개 생성되었는지 개수를 알고 싶다면 => 생성자에서 변수 값을 +1 => 소멸자에서 변수 값을 -1 ③ 객체의 개수를 관리할 변수가 필요하다. 객체의 개수를 관리할 변수를 만들어보자 #include class Car { int speed; int color; public: int cnt = 0; Car() { ++..
[C++] 객체의 복사 방법
1. 객체의 복사 방법 #include #include class Person { char* name; int age; public: Person(const char* n, int a) : age(a) { name = new char[strlen(n) + 1]; strcpy(name, n); } ~Person() { delete name; } }; int main() { Person p1("kim", 20); Person p2 = p1; } ① 얕은 복사란? (Shallow Copy) => 클래스 안에 포인터 멤버가 있을 때 디폴트 복사 생성자가 => 메모리 자체를 복사하지 않고 주소만 복사 하는 현상 #include #include class Person { char* name; int age; pub..
![[C++] 복사 생성자 (copy constructor)](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FcIMwgi%2FbtqzAh11oLg%2F37iGjSwJ36blcNca31kAU1%2Fimg.png)
[C++] 복사 생성자 (copy constructor)
1. 복사 생성자 (Copy Constructor) #include class Point { public: int x; int y; Point() : x(0), y(0) {} Point(int a, int b) : x(a), y(b) {} }; int main() { Point p1; // Point() 생성자 ok Point p2(1, 2); // Point(int a, int b) 생성자 ok Point p3(1); // Point(int a) 생성자 error Point p4(p2); // Point(Point p) 생성자 ok } 위의 코드에서 Point p4(p2); 의 경우 생성자가 없으니 error가 나야 할 것 같지만 막상 컴파일 해보면 에러 없이 정상 실행 된다. 그 말은, Point 클..
![[C++] explicit 생성자](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FN6itA%2FbtqzzKWxDxO%2Fkjp2b36WlI6vT0X1efikXK%2Fimg.png)
[C++] explicit 생성자
1. explicit 생성자(1) #include 특정 클래스 설계 시 복사 초기화를 사용하지 못하게 하는 것이 좋을 때가 있다. - 이제 아래에서 위의 코드의 문제를 해결해보자. 2. explicit 생성자(2) #include 아무 문제 없이 컴파일 된다. => 생성자가 explicit가 아니다. ② vector => 복사 초기화를 하면 에러가 난다. => 생성자가 explicit이다. => v3는 문제 없이 컴파일 된다. : v2에서 호출되는 생성자와 v3에서 호출되는 생성자는 다른 생성자다. ③ C++ 표준 스마트 포인터 => 복사 초기화를 하면 에러가 난다. => 생성자가 explicit이다.