Make Unreal REAL.
article thumbnail
if 조건문에서 지역 변수 선언
C++/기타 2023. 5. 6. 15:55

if 조건문 안에서도 해당 블록에서 사용할 수 있는 지역 변수를 선언할 수 있다. #include using namespace std; void main() { if (bool b = true) cout

article thumbnail
정수 표기법(Literal)
C++/기타 2023. 4. 19. 20:11

진법 표기를 위한 0x, 0b 등의 접두사(Prefix)가 있고, 자료형을 나타내기 위한 f, ull 등의 접미사(Suffix)가 있다. #include using namespace std; void main() { int binary = 0b1010; int octal = 012; int decimal = 10; int hexadecimal = 0xA; cout

article thumbnail
동적 할당 시 0으로 초기화
C++/기타 2023. 3. 20. 21:34

C언어에서는 calloc() 함수로 0으로 초기화된 메모리를 할당 받을 수 있다. #include #include void main() { int i; int *arr = (int*)calloc(5, sizeof(int)); for (i = 0; i < 5; ++i) printf("%d ", arr[i]); free(arr); arr = NULL; return 0; } 출력 0 0 0 0 0 How to initialise memory with new operator in C++? I'm just beginning to get into C++ and I want to pick up some good habits. If I have just allocated an array of type int with ..

article thumbnail
콤마(,) 연산자
C++/기타 2023. 3. 20. 20:32

두 개의 표현식을 콤마(,)를 통해 하나로 묶어주는 역할을 한다. 왼쪽 표현식부터 평가된다. 변수를 선언할 때 사용하는 구분자 콤마와는 다른 의미이다. #include using namespace std; void main() { int i; int j = (i = 2, i += 3); cout

article thumbnail
람다 식에서 [this] 캡처
C++/기타 2023. 3. 15. 11:12

클래스 내의 람다 식에 this 포인터를 캡처로 전달하면 람다 식 내에서 클래스의 멤버 변수와 함수를 사용할 수 있게 된다. 멤버 변수는 레퍼런스 캡처가 된다. private, public에 상관 없이 사용할 수 있다. 멤버 변수가 아닌 변수는 기존 방식대로 같이 넘겨주어야 한다. C++17부터는 *this를 전달할 경우 복사 캡처로 사용할 수 있다. #include using namespace std; class Test { public: int m_public = 1; private: int m_private = 10; public: Test() {} void f_lambda(int param) { [this, &param]() { m_public *= 2; m_private *= 2; param *..

article thumbnail
클래스 전방 선언(Forward declaration)
C++/기타 2023. 3. 7. 15:59

어떤 파일에 아래와 같이 작성되어 있는 경우 컴파일러 입장에서는 ClassA에 몇 Byte를 할당해야 할지 모르기 때문에 에러가 발생한다. class ClassA; class ClassB { ... ClassA a; }; 이런 경우 하는 수 없이 다음과 같이 ClassA의 헤더를 추가해야 하는데 #include "ClassA.h" class ClassB { ... ClassA a; }; 만약 ClassA의 헤더 안에서 ClassB의 헤더를 참조하고 있다면 헤더 꼬임 혹은 상호 참조가 발생해 문제가 생긴다. #include "ClassB.h" class ClassA { ... ClassB b; }; 클래스 전방 선언을 사용하면 헤더 파일에서 같은 모듈에 있는 다른 헤더 파일을 참조하지 않아도 되므로 상호 ..

검색 태그