분류 전체보기
-
-
[cpp] cpp module 00C++(cpp) 2021. 7. 1. 18:37
string header를 include 해야하는 이유 iostream을 include하면 string header없이도 string을 사용할 수 있다. iostream이 string header를 포함하고 있기 때문이다. 하지만 iostream에 의존해서는 안되고, 표준으로 하려면 쓰는 헤더를 다 포함해줘야 한다. [출처] Do I have to use #include beside ? I started learning C++ and I read a book which writes that I must use the header file because the string type is not built directly into the compiler. If I use the I... stackoverfl..
-
[cpp] 디폴트 인자C++(cpp) 2021. 6. 24. 15:20
생성자의 인자에 디폴트 값 주기 #include #include class Exa{ int _a; int _b; char* _str; public: Exa(int a = -1, int b = -1, char const* str = NULL){ _a = a; _b = b; if (str) { _str = new char[strlen(str) + 1]; strcpy(_str, str); } else _str = NULL; }; ~Exa(){ if (_str) delete[] _str; } void print(){ std::cout
-
[cpp] explicit, mutable 키워드C++(cpp) 2021. 6. 24. 13:04
Explicit-명시적, implicit-암시적 #include #include class Exp{ char* _str; public: Exp(char const* str){ _str = new char[strlen(str + 1)]; strcpy(_str, str); }; Exp(int a){ _str = new char[a + 1]; strlcpy(_str, (char const*)"aaaaaaaaaaa", a + 1); }; Exp(char a){ _str = new char[2]; _str[0] = a; _str[1] = '\0'; }; void print(){ std::cout
-
[cpp] 소멸자, 복사 생성자C++(cpp) 2021. 6. 24. 12:38
소멸자 생성자의 반대격, 객체가 사라질 때 호출되는 함수(자동으로)이다. 객체 내에 new로 동적 생성한 변수 등이 있을때 유용함. 생성자와의 공통점: 디폴트 생성자가 있음, 그러나 아무 행동도 하지 않음. 생성자와의 차이점: 오버로딩 불가능 형식: ~(클래스의 이름) 사용예시 #include class Example{ private: char* _name; public: // 생성자 Example(){ _name = NULL; } Example(char const* name){ _name = new char[strlen(name) + 1]; strcpy(_name, name); } // 소멸자 ~Example(){ if (_name) { std::cout