[C++] Class 공부 & instantiation (+ Struct)
이거 읽어보는게 빠름
class 공부 :
- 아무 말 없이 변수 선언하면 private이라 밖에서 접근 X;
public 명시 (근데 멤버 변수는 보통 private으로 하고, 접근 함수를 public으로 만들잖아 -> ps할땐 시간 절약 겸 보통 다 public)
- 변수 안받는 default constructor 구현한 뒤, vector<클래스>(N); 했더니 내가 원하는대로 자동으로 생성되더라.
- 클래스 내부에 멤버 함수 선언만 하고, 밖에다가 따로 정의할 때 :
리턴값 클래스::함수이름(){...}
ex) void Garden::spring() { ... }
+ this로 자기 자신 클래스 지칭 가능하나, 생략하고 써도 됨
- 콜론 초기화(:) - c++ 멤버 초기화 리스트(Member Initialize List)
https://caileb.tistory.com/165
class Garden {
private:
int food; // getFood를 통해서만 접근
public:
Garden() :food(5) {};
void spring(); // declaration만, 정의는 바깥에
int getFood() const {
return food;
} // const member function
};
void Garden::spring() {
this->food;
food; // this 생략됨
...
}
Class instantiation에 대해 (막상 한번도 해본 적 없는 것 같아서)
www.cplusplus.com/forum/beginner/166003/
class Coord {
public:
int x, y;
Coord() :x(0), y(0) {};
Coord(int _x, int _y) :x(_x), y(_y) {}; // member initializer lists
Coord operator+ (const Coord& addend) {
return Coord(x + addend.x, y + addend.y);
}
...
}
ostream& operator<< (ostream& os, const Coord& pt) {
return os << "{" << pt.x << ", " << pt.y << "}";
}
int main() {
Coord pt1(1, 2); // 일반
Coord* pt2_ptr = new Coord(5, 4); // 동적 할당 (new) - 포인터 반환
cout << pt1 << "\n" << *pt2_ptr << "\n"; // {1, 2} {5, 4}
cout << pt1 + *pt2_ptr << "\n"; // {6, 6}
}
특히 struct instantiation 헷갈림
https://stackoverflow.com/questions/5914422/proper-way-to-initialize-c-structs
C c = C(); // init using default constructor
C* cptr = new C();
C c; // init using default constructor (like line#1)
C c(1, 2, 3); // init using predefined constructor (just like class)
struct vs class는 나의 다른 글 참고
https://goldenriver42.tistory.com/34?category=944792
(31Mar22) Q. How do I delete a statically instantiated object (instance of a class)? I don't have a pointer to free/delete...
Car c1(500, 10);
// How to 'delete' c1? Cannot use free/delete since c1 is not a pointer...
https://stackoverflow.com/questions/36192499/how-do-i-manually-delete-an-instance-of-a-class
local variable is stored in the stack memory → cleared when the function ends
You still want to delete the data on the stack memory → use 'memset' to clear memory to 0
* Part where I got confused) Since C doesn't have a garbage collector, would static data just stay on the memory all the time, until the programmer explicitly frees it?
→ this applies only to the dynamic allocation. static allocation is cleared by the program automatically.
(28May22) Create(instantiate) an object without name
→ 가능!
routerNumToIPs.push_back(std::vector<in_addr_t>()); // 네떡
https://stackoverflow.com/questions/47223913/create-an-object-without-a-name-in-c
참고) copying an object (Shallow copy vs deep copy) → copy constructor for class
https://www.geeksforgeeks.org/shallow-copy-and-deep-copy-in-c/
'<언어> > [C++]' 카테고리의 다른 글
[Modern C++][C++11] push_back vs emplace_back (0) | 2021.07.16 |
---|---|
[Modern C++][C++11] nullptr (0) | 2021.05.11 |
[Modern C++][C++11] Rvalue Reference and move semantics (0) | 2021.02.13 |
[C++] Operator overloading (연산자 오버로딩) (0) | 2021.02.08 |
[C++] Type Conversion 정리 (0) | 2020.12.07 |