이거 읽어보는게 빠름

https://boycoding.tistory.com/category/C++%20%EC%9D%B4%EC%95%BC%EA%B8%B0/09.%20%EA%B0%9D%EC%B2%B4%EC%A7%80%ED%96%A5%20%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%B0%8D

 

'C++ 이야기/09. 객체지향 프로그래밍' 카테고리의 글 목록

소년코딩, 자바스크립트, C++, 물리, 게임 코딩 이야기

boycoding.tistory.com

 

 

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 instantiation, which is the best w - C++ Forum

Line 6 is undefined behaviour (most likely, a crash), because you haven't initialised test1 to actually point to anything. Your creation and use of test2 and test3 are fine. It all depends what you want the lifetime of the object to be. Do you need to crea

www.cplusplus.com

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

 

Proper way to initialize C++ structs

Our code involves a POD (Plain Old Datastructure) struct (it is a basic c++ struct that has other structs and POD variables in it that needs to get initialized in the beginning.) Based one what I...

stackoverflow.com

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://wikidocs.net/20082

https://www.geeksforgeeks.org/shallow-copy-and-deep-copy-in-c/

 

 

 

+ Recent posts