본문 바로가기
반응형

개발/C, C++67

C++ - 템플릿으로 클래스 상속하기 (Mixin Inheritance) C, C++ 전체 링크 참고 - 데코레이터 패턴 클래스 CBA가 클래스 A, B, C의 모든 메서드를 사용하려면 상속을 받으면 된다. class CBA : public A, public B, public C { }; 예제 코드는 다음과 같다. #include using namespace std; class A { public: void methodA() { cout 2024. 3. 6.
C++ - CRTP로 인터페이스 만들기 (Interface with Curiously Recurring Template Pattern) C, C++ 전체 링크 참고 - 형변환 연산자 - 가상 함수 테이블 C++의 인터페이스는 순수 가상 함수를 이용해서 만들 수 있다. class Base { public: virtual void interface() = 0; // 순수 가상 함수 void commonOperation() { cout 2024. 3. 6.
C++ - friend로 private 멤버 출력하기 (Printing private members using friend) C, C++ 전체 링크 참고 - 연산자 오버로딩을 이용하여 구조체 출력하기 - 스마트 포인터 : unique_ptr 아래 코드를 실행하면 컴파일 에러가 발생한다. #include #include using namespace std; class Bread { public: Bread(string n, int p) : name(n), price(p) {} string getName() const { return name; } void setName(const string &n) { name = n; } int getPrice() const { return price; } void setPrice(const int p) { price = p; } private: string name; int price; };.. 2024. 2. 13.
C++ - atomic으로 원자적 연산 처리하기 (vs mutex) C, C++ 전체 링크 원자성 (Atomicity) - 연산이 도중에 중단되지 않고 전체가 완료되거나 전혀 수행되지 않는 것 - 원자성이 지켜져야 멀티스레드 환경에서 데이터의 일관성을 유지하고 동시성 문제를 해결할 수 있다. 다음 코드를 실행해 보자. #include #include using namespace std; int counter; void incrementCounter() { for (int i = 0; i < 1000000; i++) counter++; } int main() { thread t1(incrementCounter); thread t2(incrementCounter); t1.join(); t2.join(); cout 2024. 1. 26.
C++ - call_once로 함수를 한 번만 호출하기 C, C++ 전체 링크 test 함수는 처음 호출 되는 경우에만 init을 수행한다. #include using namespace std; bool first; void test() { if (first == false) { first = true; cout 2024. 1. 24.
C++ - 정수를 IP로 변환하기 (IP Converter for IPv4, IPv6, Integer, in_addr, in6_addr) C, C++ 전체 링크 참고 - https://www.vultr.com/resources/ipv4-converter/ - https://regexr.com/ - 연산자 오버로딩을 이용하여 구조체 출력하기 - IPv4 Invalid Check - IPv6 Invalid Check (+ 축약형) - Integer to IPv4 String - IPv4 String to Integer - IPv4 String to Integer / Integer to IPv4 String with - in_addr to IPv4 String / IPv4 String to in_addr with - in6_addr to IPv6 String / IPv6 String to in6_addr with - IPv4 to IPv6 wi.. 2023. 11. 28.
C++ - 연산자 오버로딩을 이용하여 구조체 출력하기 (cout with overloading) C, C++ 전체 링크 C++에서는 2023. 11. 22.
C++ - map, functional로 함수 호출하기 C, C++ 전체 링크 string 명령어로 주어진 함수를 호출한다고 가정해 보자. 예를 들어 test2 함수를 호출하려면 아래와 같이 선언한다. string cmd = "test2"; if / else를 이용하여 아래와 같이 함수를 호출할 수 있다. #include #include using namespace std; void test1() { printf("%s\n", __FUNCTION__); } void test2() { printf("%s\n", __FUNCTION__); } void test3() { printf("%s\n", __FUNCTION__); } int main(void) { string cmd = "test2"; if (cmd.compare("test1") == 0) { test1.. 2023. 11. 22.
C++ - 폴더, 파일 관리 함수 정리 with sys/stat.h, dirent.h, fstream C, C++ 전체 링크 참고 - Window Visual Studio에서 폴더의 모든 파일 통합하기 - 폴더, 파일 확인 함수 - 지정한 경로의 폴더와 파일을 가져오는 함수 - 지정한 경로의 모든 폴더와 파일을 가져오는 함수 (recursive) - 파일 존재 여부 확인 - 파일 확장자 확인 - 파일 create, read, update - 파일 delete - 폴더 생성 - 하위 폴더 포함하여 모든 폴더 만들기 (recursive, mkdir -p 옵션) - 폴더 삭제 (빈 폴더인 경우, rmdir) - 폴더 삭제 (하위 폴더, 파일 모두 포함, rm -r) 테스트는 replit에서 가능하다. 폴더, 파일 확인 함수 #include #include #include using namespace std; .. 2023. 9. 9.
반응형