월 24,200원
5개월 할부 시다른 수강생들이 자주 물어보는 질문이 궁금하신가요?
- 미해결홍정모의 따라하며 배우는 C++
[14:55] 교수님 코드와 다르게 move constructor도 호출되었는데요.
안녕하세요. 여기서 무브 컨스트럭터는 호출 될 수도 있는 건가요? 리소스의 copy assignment는 사용이 안되는 걸 확인 할 수 있는데 결과가 좀 달려서 여쭤봅니다. 감사합니다. <메인 전체코드> // 15_3.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include "Timer.h" #include "AutoPtr.h" #include "Resource.h" AutoPtr<Resource> generateResource() { AutoPtr<Resource> res(new Resource(10'000'000)); return res; } int main() { using namespace std; streambuf* orig_buf = cout.rdbuf(); //cout.rdbuf(NULL); // disconnect cout from buffer Timer timer; { AutoPtr<Resource> main_res; main_res = generateResource(); } cout.rdbuf(orig_buf); timer.elapsed(); return 0; } <오토피티알 전체코드> #pragma once #include <iostream> template<class T> class AutoPtr { private: T* m_ptr; public: AutoPtr(T* ptr = nullptr) : m_ptr(ptr) { std::cout << "AutoPtr default constructor " << std::endl; } ~AutoPtr() { std::cout << "AutoPtr destructor " << std::endl; if (m_ptr != nullptr) delete m_ptr; } //AutoPtr(const AutoPtr& a) //{ // std::cout << "AutoPtr copy constructor " << std::endl; // // deep copy // m_ptr = new T; // *m_ptr = *a.m_ptr; //} //AutoPtr& operator=(const AutoPtr& a) //{ // std::cout << "AutoPtr copy assignment " << std::endl; // if (&a == this) // preent self-assignment // return *this; // if (m_ptr != nullptr) delete m_ptr; // // deep copy // m_ptr = new T; // *m_ptr = *a.m_ptr; // return *this; //} //AutoPtr(const AutoPtr& a) = delete; //AutoPtr& operator=(const AutoPtr& a) = delete; AutoPtr(AutoPtr&& a) : m_ptr(a.m_ptr) { a.m_ptr = nullptr; // really necessary? std::cout << "AutoPtr move constructor " << std::endl; } AutoPtr& operator=(AutoPtr&& a) { std::cout << "AutoPtr move assignment " << std::endl; if (&a == this) // prevent self-assignment return *this; if (!m_ptr) delete m_ptr; // shallow copy m_ptr = a.m_ptr; a.m_ptr = nullptr; //really necessary? return *this; } };
- 해결됨홍정모의 따라하며 배우는 C++
[09:51] 음성 오퍼레이터 오버로딩 -> 함수 오버로딩
안녕하세요? 해당 부분음성이 함수 오버로딩으로 바뀌어야 할 거 같아요. 감사합니다.
- 미해결홍정모의 따라하며 배우는 C++
[12:39] res1, res2가 지워지는 순서에 대한 질문
안녕하세요? [12:39] 음성이 res2가 지우려고 할 때 문제가 생긴다고 되어있는데 AutoPtr이라서 res1이 먼저 지우고, res2가 나중에 지우는 거죠? 이게 헷갈릴 수 있는 문제인가 의문이 드네요 ㅠㅠ. 아마도 소멸자가 호출 순서는 생성자의 호출 순서와 반대라는 거 때문에 헷갈리는 거 같은데 오토피티알이라서 호출 순서는 상관 없는 거죠? 감사합니다. ============자답?=========== 정확한지는 모르겠는데 나중에 실행한 게 먼저 소멸되는 거 같습니다.
- 미해결홍정모의 따라하며 배우는 C++
[02:41] s.resize(-1) 이 던지는 익셉션을 받아서 what()을 출력했을 때 string too long이 나오는 이유
안녕하세요? 복습하면서 다시 생각해봤습니다. 혹시 -1로 리사이즈를 한다면 뭐가 들어오든 리사이즈 된 사이즈보다는 길테니까 그런건가요? ==================================자답========== unsigned라서 -1을 하면 0xffffffff 가 되니까 그런거네요 ㅠ. 감사합니다.
- 미해결홍정모의 따라하며 배우는 C++
vcpkg 설치 오류로 문의드립니다.
안녕하세요 교수님! 질문 처음 올려보는데 이런 오류 문제로 처음 올리게 되네요. 다름이 아니라 20.4 vcpkg 설치 과정 강의를 들으면서 설치를 따라하고 있는데 vcpkg install boost:x64-windows 명령어를 실행할 때마다 계속 이러한 설치 오류가 납니다. 구글링 하여 여러 방법을 따라 시도해 보았지만 해결이 되지 않고 있습니다 ㅠㅠ 게임 만들기 강의도 오늘부터 수강 시작하려고 했던 참이라 문제가 빨리 해결 되었으면 좋겠는데... 도움 주시면 감사하겠습니다. 비쥬얼 스튜디오 2017 버전을 사용하고 있습니다. 연휴 잘 보내시고 새해 복 많이 받으세요!
- 해결됨홍정모의 따라하며 배우는 C++
[아래질문 관련] 리턴타입이 int일 때와 Cents일 때의 차이
안녕하세요. 출력연산자 오버로딩을 해놔서 아래 처럼만 오버로딩해도 체이닝되는 것 처럼 보였던 거 같아요. int operator-() { return -m_cents; } 출력연산자 오버로딩을 주석처리하고 프린트 함수를 만들어서 테스트해보니 int를 리턴해서는 체이닝이 안되네요. void print() { cout << m_cents << endl; } /*friend std::ostream& operator<<(std::ostream& out,const Cents& cents) { out << cents.m_cents; return out; }*/ 감사합니다. <전체코드> // 9_3.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include <iostream> using namespace std; class Cents { private: int m_cents; public: Cents(int cents_in = 0) : m_cents(cents_in) {} int getCents() const { return m_cents; } int& getCents() { return m_cents; } void print() { cout << m_cents << endl; } /*friend std::ostream& operator<<(std::ostream& out,const Cents& cents) { out << cents.m_cents; return out; }*/ //int operator-() //{ // return -m_cents; //} Cents operator-() const { return Cents(-m_cents); } bool operator!() const { return (m_cents == 0) ? true: false; } }; int main() { Cents cents1(6); Cents cents2(2); (-(-(-cents1))).print(); return 0; }
- 미해결홍정모의 따라하며 배우는 C++
[아래질문 에러메시지]
안녕하세요? 아래와 같이 에러메시지가 나옵니다. 기본 멤버 이니셜 라이저에서 배열 바운드를 추론 할 수 없습니다 ========= 아래 =============== 검색해서 아래와 같은 정보를 찾았습니다. https://stackoverflow.com/questions/29593207/what-is-the-reason-for-not-being-able-to-deduce-array-size-from-initializer-stri 여기에 아래와 같은 답변들을 보니까 Foo라는 구조체의 생성자의 멤버 이니셜라이제이션 리스트에서 초기화 될 수 있기 때문이라고 설명하는 거 같습니다. because str can be also initialized in the member-initialization-list of Foo's constructor, discarding the initializer from the in-class initializer The reason is that you always have the possibility to override an in-class initializer list in the constructor. So I guess that in the end, it could be very confusing. struct Foo { Foo() {} // str = "test\0"; // Implementing this is easier if I can clearly see how big `str` is, Foo() : str({'a','b', 'c', 'd'}) {} // str = "abcd0" const char str[] = "test"; }; Notice that replacing const char with static constexpr char works perfectly, and probably it is what you want anyway. 감사합니다.
- 해결됨홍정모의 따라하며 배우는 C++
[ 02:10] 프라이빗에서 퍼블릭으로 바꿔다는 게 한가지가 있고요
안녕하세요? 새해 복 많이 받으세요. 제가 내용을 잘못 적었었네요. private에서 퍼블릭으로 바꿔서 인캡슐레이션이 깨진 부분을 설명해주고 계신데요. 음성은 "퍼블릭에서 프라이빗으로 바꿔다는 게 한가지가 있고요" 이렇게 나오고 있어요. 이 부분이 "프라이빗에서 퍼블릭으로 바꿨다는 게 한가지가 있고요" 로 되어야할 거 같습니다. 감사합니다.
- 해결됨홍정모의 따라하며 배우는 C++
[04:10] 음성 protected는 자식도 접근 가능하다라고 바뀌어야 할 거 같아요.
안녕하세요? [04:10] 음성 protected는 자식도 접근 가능하다라고 바뀌어야 할 거 같아요. 감사합니다.
- 해결됨홍정모의 따라하며 배우는 C++
[12:31] 1. Person(name_in) ide에서 순간적으로 잘못 판단한 거 같아요. 2. 음성문제
안녕하세요? 복습을 하면서 느끼는 건데 강의에 정말 정성이 많이 들어간 것 같아요. 감사하게 생각하고 있습니다. 1. =========================================== Person의 생성자에 디폴트 파라미터를 넣지 않아도 정상적으로 작동합니다. 아마 순간적으로 비주얼스튜디오가 잘못 판단했던 거 같습니다. 강의화면에 나온 오류메시지를 보고 실험을 해봤는데요.아규먼트 타입이 안맞았다는 오류 같아요. 강의화면을 보면 요 오류메시지가 Person::Person에 아규먼트 타입이 (const std::string)과 일치하는 생성자가 없다? 라고 말하는 거 같은데 바로 넘어간 화면을 보면 이렇게 const std::string을 매개변수로 받는 생성자가 있거든요. 저는 const를 빼고 실험을 해보니까 같은 오류가 뜨더라고요. const를 다시 붙이니까 오류가 사라지고요. 그래서 일시적인 ide의 판단착오? 아닐까 생각을 해봤습니다. 2.========================================================== [30:00] 음성 바뀌어야할 것 같습니다. "Person클래스에 있는 것들은 student class와 teacher class에서 가져다 쓸 수가 있어요" 말이 바뀌어서 나와버린 거 같아요. 감사합니다.
- 해결됨홍정모의 따라하며 배우는 C++
[연습문제] 이니셜라이저 리스트 , 대입연산자 오버로딩
안녕하세요? 이 경우에는 파라미터?로 이니셜라이저 리스트를 받으니까 1. 셀프 어사인먼트 방지하는 방법은 생각을 못했습니다. IntArray& operator=(const std::initializer_list<int>& list) { cout << "assignment operator" << endl; cout << "list.size() In assignment operator " << list.size() << endl; delete[] m_data; int length = list.size(); m_data = new int[length]; int count = 0; for (auto& element : list) { m_data[count] = element; ++count; } return *this; } //IntArray(list.size()); // 생성자를 통한 초기화는 안되는거고 2. m_data가 데이터를 가지고 있을 수는 있으니까 지워주고 리스트의 길이만큼 new로 초기화해주고 리스트의 값을 복사해줬습니다. 교재인 learncpp에서 해당 부분을 찾아보니 다음과 같이 출력을 해야 한다더라고요. 그런데 리스트의 사이즈는 7인데 5개만 나왔습니다. 자고 일어나서 해결해보겠습니다. 감사합니다. <출력화면> <전체코드> // 9_12.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include <iostream> #include <cassert> #include <initializer_list> using namespace std; class IntArray { private: unsigned m_length = 0; int* m_data = nullptr; public: IntArray(unsigned length = 0) : m_length(length) { m_data = new int[length]; } IntArray(const std::initializer_list<int>& list) : IntArray(list.size()) { cout << "list.size() In constructor " << list.size() << endl; int count = 0; for (auto& element : list) { m_data[count] = element; ++count; } //for (unsigned count = 0; count < list.size(); ++count) // m_data[count] = list[count]; // error } ~IntArray() { delete[] this->m_data; } IntArray& operator=(const std::initializer_list<int>& list) { cout << "assignment operator" << endl; cout << "list.size() In assignment operator " << list.size() << endl; delete[] m_data; int length = list.size(); m_data = new int[length]; int count = 0; for (auto& element : list) { m_data[count] = element; ++count; } return *this; } friend std::ostream& operator << (std::ostream& out, const IntArray& arr) { for (unsigned i = 0; i < arr.m_length; ++i) out << arr.m_data[i] << " "; out << endl; return out; } }; int main() { int my_arr1[5] = { 1,2, 3,4,5 }; int* my_arr2 = new int[5]{ 1,2,3,4,5 }; auto il = { 10, 20, 30 }; IntArray int_array { 1, 2, 3, 4, 5 }; cout << int_array << endl; int_array = {7, 2, 3, 2, 1, 6, 5}; cout << int_array << endl; return 0; }
- 미해결홍정모의 따라하며 배우는 C++
extern 변수
안녕하세요. 4.2강의 뒷부분에서 의문증이 생겼습니다. 외부 연결에서 MyConstants.h 헤더파일에 pi를 선언하고 MyConstants.cpp에 초기화를 해줬잖아요 그런데 어차피 cpp파일에 namespace Constants가 정의되어 있고 초기화까지 되어 있는데 헤더파일이 굳이 필요할까? 의문이 들어서 헤더파일을 포함하지 않고 실행을 했는데 실행이 되지 않네요. 이유가 무엇인지 궁금합니다. 감사합니다.
- 해결됨홍정모의 따라하며 배우는 C++
[04:05] 증감연산자(후위) 오버로딩할 때 지역변수 temp
안녕하세요? 증감연산자(후위) 오버로딩 할 때 지역변수 temp를 리턴하는 거라서 이것도 문제가 될 수 있는 건가요? 감사합니다.
- 해결됨홍정모의 따라하며 배우는 C++
[01:27] 단항연산자(-) 오버로딩
안녕하세요? 단항연산자(-) 오버로딩 할 때 여기서는 이렇게만 해줘도 되는건가요? 감사합니다.
- 해결됨홍정모의 따라하며 배우는 C++
[02:10] 음성 private -> public ===> public -> private
안녕하세요? 1. ============================================================== [02:10] 음성 private -> public ===> public -> private 으로 바뀌어야 할 거 같아요. 그리고 혹시 교수님 기본서로 만들고 싶은데 learncpp.com의 내용을 인쇄해서 가지고 계신건가요? 가끔 교재 같은 거 ? 넘기는 소리가 들려서요. 2. =========================================================== [16:18] 에 나오는 Fraction one_third = Fraction{ 1, 3 }; 얘도 임시객체죠? 감사합니다.
- 미해결홍정모의 따라하며 배우는 C++
[13:02] 구조체 안에서 어레이 초기화할 때
안녕하세요? 기본적으로 c++에서 정적 어레이를 초기화할 때 int arr[] = {9, 7, 5, 3, 1}; 요렇게 해주면 [] 안에 사이즈를 안넣어줘도 된다라고 알고있는데 구조체 안에서는 이렇게 안되는건가요? incomplete type is not allowed라고 나옵니다. 감사합니다.
- 해결됨홍정모의 따라하며 배우는 C++
[연습문제] 재귀호출이요해서 피보나치수열 출력하기
안녕하세요? fibonacci(3)이라면 fibonacci(1) + fibonacci(2)를 호출하고 fibonacci(2) 가 또 fibnacci(0) + fibonacci(1)을 호출해서 fibonacci(0) + fibonacci(1) 이 끝나고 나오면서 0 + 1 1 + 0 + 1 이렇게 했습니다. 감사합니다.
- 해결됨홍정모의 따라하며 배우는 C++
[05:03] 함수 선언부, 정의부 중 한 곳에만 디폴트 파라미터 넣을 경우
안녕하세요? 함수 선언부에선 디폴트 파라미터 빼고 정의부에서 디폴트 파라미터 넣을 경우에 컴파일 에러가 납니다. <코드> // 7_8.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include <iostream> using namespace std; //void print(int x = 7, int y = 8, int z = 10); void print(int x, int y, int z); int main() { print(20); //print(); return 0; } void print(int x = 7, int y = 8, int z = 10) //void print(int x, int y, int z) { cout << x << " " << y << " " << z << endl; } 'print': function does not take 1 arguments 감사합니다.
- 해결됨홍정모의 따라하며 배우는 C++
[01:59] 음성 매개변수로 함수자체를 -> 매개변수로 변수(x) 자체를
안녕하세요? 1. 음성 매개변수로 함수자체를 -> 매개변수로 변수(x) 자체를 이 되어야 할 거 같아요. 2. 9분 11초에 레퍼런스가 훨씬 좋다는 내용은 복사가 안되니까 메모리 효율 관점에서 좋다는 거죠? 3. [11:15] 교수님 여러개의 리턴타입은 19.2로 뒤쪽에 있습니다. 7.5에서도 언급됩니다. 감사합니다.
- 해결됨홍정모의 따라하며 배우는 C++
[15:39] strcmp, strcat을 스스로 구현해보기
안녕하세요? 교수님 정말 그 동안 노력 안 했던 거 탓하면서 처절하게 풀었어요 . 그래도 열심히 하면 조금이라도 좋아지겠지 하면서요! 그냥 결과 끼워맞추기인데요. <strcmp> 1. 길이가 다르면 다른 문자열이니까 -1을 리턴해라 2. 길이가 같다면 그 길이 만큼 돌면서 각 배열의 같은 위치의 문자를 하나씩 비교해서 다른 게 있으면 -1을 리턴하고 다른 게 없으면 0을 리턴한다. 머리가 복잡해서 myStrlen 함수를 먼저 만들어서 길이 비교를 했고요. 그걸 이용해서 myStrcmp를 만들었어요. <결과화면> <코드> // strcmp_self.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include <iostream> using namespace std; void Compare(int input); int myStrcmp(const char arr[],const char arr2[]); int myStrlen(const char arr[]); int main() { char name1[] = "ABCDEF"; char name2[] = "ABCDEF"; char name3[] = "AbCDEF"; cout << name1 << " == " << name2 << " ? " << endl; Compare(myStrcmp(name1, name2)); cout << name1 << " == " << name3 << " ? " << endl; Compare(myStrcmp(name1, name3)); //cout << "strcmp(name1, name2) : " << strcmp(name1, name2) << endl; return 0; } int myStrlen(const char arr[]) { int count = 0; while (1) { if (arr[count] == '\0') break; count++; } return count; } int myStrcmp(const char arr[], const char arr2[]) { int result = 0; if (myStrlen(arr) != myStrlen(arr2)) { result = -1; } else { for (int i = 0; i < myStrlen(arr); ++i) { if (arr[i] != arr2[i]) result = -1; else result += 0; } } return result; } void Compare(int input) { if (input == 0) { cout << "Equal" << endl; } else { cout << "Not Equal" << endl; } } <strcat> <결과화면> <코드> // concatenate_test.cpp : This file contains the 'main' function. Program execution begins and ends there. // #define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; int myStrlen(const char arr[]); char* myStrcat(const char* x, const char* y); int main() { char str1[] = "Jack"; char str2[] = " Jack"; //strcat(str1, str2); cout << myStrcat(str1, str2) << endl; //cout << str1 << endl; return 0; } char* myStrcat(const char* x, const char* y) { //cout << myStrlen(x) + myStrlen(y) + 1 << endl; int length = myStrlen(x) + myStrlen(y) + 1; //char concatenatedString[20]{ '\0',}; char* concatenatedString = new char[length] {'\0',}; //concatenatedString[0] = 'H'; //concatenatedString[1] = 'I'; for (int i = 0; i < myStrlen(x); ++i) { concatenatedString[i] = x[i]; } for (int j = 0; j < length; ++j) { concatenatedString[myStrlen(x) + j] = y[j]; } //delete[] concatenatedString; /*concatenatedString[5] = y[1]; concatenatedString[6] = y[2];*/ return concatenatedString; } int myStrlen(const char arr[]) { int count = 0; while (1) { if (arr[count] == '\0') break; count++; } return count; } 감사합니다.