일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
- operator overloading
- new&delete
- std::endl
- std::cout
- dynamic_cast
- suffix return type
- discord bot
- pointer to member data
- member function pointer
- increment operator
- virtual function table
- constructor
- 더 지니어스 양면포커
- base from member
- placement new
- virtual function
- this call
- diamond inheritance
- vector size
- c++ basic practice
- std::vector
- virtual inheritance
- delete function
- conversion constructor
- virtual destructor
- vector capacity
- std::ostream
- c++ multi chatting room
- return by reference
- C++
- Today
- Total
목록C++/Intermediate (7)
I'm FanJae.
1. Max Implement#include #include templateconst T& mymax(const T& obj1, const T& obj2){ return obj1 1-1. 알고리즘 함수가 사용하는 정책(비교방식)을 변경하는 방법.① 알고리즘 함수가 사용하는 정책(비교방식)을 변경하고 싶을때 보통 이항 조건자를 사용한다. (C++ STL)std::sort(v.begin(),v.end(), [](auto &a, auto&b) { return a.size() #include #include templateconst T& mymax(const T& obj1, const T& obj2){ return obj1 b.size(); } ); auto ret4 = mymax(s1, ..

1. Member Function Pointer 1-1. 함수 포인터와 멤버 함수class X{public: void mf1(int a) {} // void mf1(X* this, int a) static void mf2(int a) {}};void foo(int a) {}int main(){ void(*f1)(int) = &foo; // ok// void(*f2)(int) = &x::mf1; // error; void(*f3)(int) = &x::mf2; // ok void(X::*f2)(int) = &X::mf1; // ok}- 일반 함수 포인터에 멤버 함수의 주소를 담을 수 없다.- 일반 함수 포인터에 static 멤버 함수는 담을 수 있다. ※ 일반 ..

1. this call 1-1. 멤버 함수의 의문점.class Point{ int x{0}; int y{0};public: void set(int a, int b) { x = a; y = b; }};int main(){ Point pt1; Point pt2; pt1.set(10, 20); pt2.set(10, 20);}- 멤버 데이터는 객체당 한 개씩 생성된다.- 멤버 함수는 코드 메모리에 한 개만 만들어져 있다. - 객체가 여러 개 생성되어도 멤버 함수는 한 개만 있다.※ 함수 인자는 2개 (a, b) 밖에 없는데, x가 어떤 객체의 멤버 인지(pt1.x인지 pt2.x 인지) 어떻게 아는가? 1-2. 어떤 변환이 일어나는가?clas..

1. Trivial 1-1. Special Member Function- 사용자가 제공하지 않으면 컴파일러가 제공하는 멤버 함수가 존재한다.① 디폴트 생성자(Default Constructor)② 소멸자(Destructor)③ 복사 생성자(Copy Constructor)④ 복사 대입연산자(Copy Assignment)⑤ 이동 생성자(Move Constructor)⑥ 이동 대입연산자(Move Assignment) ※ 보통 이러한 멤버 함수들을 Trivial 하다고 한다.2. Trivial Default Constructor※ 예제가 아주 많아서 check 함수는 한번만 보이고, 이후 부터는 임의 생략한다.① The constructor is not user-provided② T has no virtual ..

1. new와 delete 1-1. new와 delete의 원리#include class Point{ int x, y;public: Point(int a, int b) : x{a}, y{b} { std::cout Point* p1 = new Point(1,2);- 위와 같이 쓰면 크게 2가지 작업을 진행한다. 즉, new를 실행하면 아래와 같은 2가지가 실행되는 것이다. ① 메모리할당 void *p = operator new(sizeof(Point))② 생성자호출 Point *p1 = new(p) Point(1,2);- 여기서 new(p) Point(1,2); 와 같은 표기법을 placement new라고 한다. delete p1;- 위와 같이 쓰면 크게 2가지 작업을 진행한다. 즉, delet..

1. 변환 연산자와 변환 생성자 1-1. 변환 연산자#include class Int32;{ int value;public: Int32() : value(0) { } operator int() const { return value; }};int main(){ int pn; // primitive type Int32 un; // user type pn = un; // un.operator int() un = pn; // pn.operator Int32() 이는 불가능 // 1. un.operator=(pn) // 2. Int32(pn)}- 객체가 다른 타입으로 변환 될 때 호출되는 함수이다. operator TYPE(){..

1. 생성자(Constructor)의 생성 원리1-1. 생성자, 소멸자 호출의 정확한 원리#include struct BM // BaseMember{ BM() { std::cout - 위와 같은 코드가 존재한다고 가정하자. 그러면 실제로는 아래와 같이 변환해준다.struct Base{ BM bm; Base() : bm() { } Base(int a) : bm() {} ~Base() {...; bm.~BM(); }};struct Derived : public Base{ DM dm; Derived() : Base(), dm() { } Derived(int a) : Base(), dm() { } ~Derived..