skf346
@skf3464773
Reviews Written
1
Average Rating
5.0
Posts
Q&A
Buffer overrun ๊ด๋ จํด์ ์ง๋ฌธ๋๋ฆฝ๋๋ค!
์๋ ํ์ธ์!์ผ๋จ ์ ๋ VS2022 ์ฌ์ฉํ๊ณ ์๊ณ ์ผ๋ง์ stackoverflow๋ฅผ ์ฐพ์๋ณธ ๊ฒฐ๊ณผ vs์์ ๋ฐ์ํ๋ ์ผ์ข ์ ๋ฒ๊ทธ๋ผ๊ณ ์๊ฐ๋ฉ๋๋ค!https://stackoverflow.com/questions/64713842/any-way-to-avoid-warning-c6386-without-disabling-it-or-code-analysis-altogetherComplier explorer๋ฅผ ํตํด gcc ๋ฑ ์ปดํ์ผ๋ฌ๋ก ์คํํด๋ดค๋๋ฐ warning์ด ์กํ์ง ์์์ต๋๋ค!VS code analyzer๊ฐ ์ ์ฝ๋์ list.size()๊ฐ 0๋ณด๋ค ํฐ ์ง ํ์คํ๊ฒ ํ๋จํ๊ธฐ ์ด๋ ค์์ ํด๋น ๋ฒ๊ทธ๊ฐ ๋ฐ์ํ๋๊ฒ์ผ๋ก ์ถ์ ๋ฉ๋๋ค!์ด๋ฅผ max(list.size(), 0)์ ์ฌ์ฉํ ํธ๋ฆญ์ผ๋ก ์์จ ์๋ ์๋ค๊ณ ๋ ํ๋๋ฐ ๊ทผ๋ณธ์ ์ธ ํด๊ฒฐ์ฑ ์ ์ ํํ ๋ชจ๋ฅด๊ฒ ๋ค์.
- 0
- 2
- 492
Q&A
cout<<mon ์ง๋ฌธ
๊ต์๋๊ป์ Position header์์ Position2D class์ ๋ํด์๋ ๋ค์ ํ๋ฒ ํ์ธํด ๋ณด์๋ฉด ์ดํดํ์ค ๊ฑฐ์์!
- 0
- 2
- 258
Q&A
<< ์ค๋ฒ๋ก๋ฉ ์ const ์ ๋ฌด์ ๋ฐ๋ฅธ ๋ฌธ์
d++์ ๊ฒฝ์ฐDigit operator ++ (int){Digit temp(m_digit);++(*this);return temp;}์์ ๋ฐํํ๋ ๊ฐ์ ๊ธฐ๋ณธ์ ์ผ๋ก rvalue์ ๋๋ค. pointer๋ reference ๋ฐํ์ ํ์ง ์๋ ์ด์ ํจ์๋ฅผ ํธ์ถํ expression์ return by value๊ฐ rvalue๋ก "์ ์" ๋์ฒด ๋๋ ๊ฒ์ ๋๋ค. d++์์ ์ฃผ์๋ฅผ ํ์ธํ๊ธฐ ์ํด &๋ฅผ ๋ถ์ฌ๋ณด๋ฉด rvalue์์ ํ์ธํ ์ ์์ต๋๋ค.rvalue์ ๊ฒฝ์ฐ์๋ ํจ์ parameter๊ฐ reference๋ก ๋ฐ์ ๋(pass by reference)๋ const๋ฅผ ๊ผญ ๋ถ์ฌ์ฃผ์ด์ผ ํฉ๋๋ค. ๋ฐ๋ผ์ const reference๋ rvalue์ ์๋ช ์ ์ฐ์ฅ์์ผ ์ค๋ค๊ณ ํฉ๋๋ค. ์ด์ ๊ด๋ จํด์๋ ์๋๋งํฌ๋ฅผ ํตํด ํ์ธํ ์ ์์ต๋๋ค! https://stackoverflow.com/questions/36102728/why-is-it-allowed-to-pass-r-values-by-const-reference-but-not-by-normal-referenc?newreg=af09de2f1d8d479ca830d2af2a5cb732
- 0
- 2
- 274
Q&A
ํ์ ์ฐ์ฐ์์์ this๊ฐ ์๋ ๋ค๋ฅธ ์ธ์คํด์ค์ ์ ์ฃผ์๊ฐ ์๋ ๊ฐ์ ๋๊ฒจ์ฃผ๋ฉด ์๋๋์?
Digit& operator ++ (int) { Digit temp(m_digit); ++(*this); return *temp; }ํจ์ ์์์ ์ ์๋ temp๋ผ๋ instance์ scope๋ ํจ์ ๋ด๋ถ๊ฐ ๋ฉ๋๋ค. ํจ์๋ฅผ ๋ฒ์ด๋๊ฒ ๋๋ฉด ์๋ฉธํ๊ฒ ๋๋ ๊ฑฐ์ฃ . reference๋ก ๋ฐํ์ ํ๋ ค๋ฉด ์๋ณธ์ด ์กด์ฌํด์ผํ๋๋ฐ ํจ์ ๋ฐ์ ๋๊ฐ๋ฉด ์ฌ๋ผ์ง๋ temp๋ฅผ return by referenceํ๋ฉด reference๋ ์๋ณธ์ ์ฐพ์ ์ ์์ต๋๋ค.
- 0
- 2
- 220
Q&A
์ด๊ธํ ์ง๋ฌธ์ด๋ผ ์ฃ์กํฉ๋๋ค.
์ ๋ถ ๋ง์์ฒ๋ผ rvalue์ ๊ด๋ จ์ด ์์ต๋๋ค.-cents๋ Cents(-m_cents)๋ฅผ returnํ๋๋ฐ ์ด๊ฑด ์ต๋ช ๊ฐ์ฒด์ด์ฃ . ์ต๋ช ๊ฐ์ฒด(Cents(10) ๋ฑ)๋ rvalue๋ก ์ฌ๊ฒจ์ง๋๋ฐ ํจ์๋ก pass rvalue๋ฅผ ํ๋ ค๋ฉด parameter๋ const reference๋ง ๊ฐ๋ฅํฉ๋๋ค.rvalue๋ฅผ ์ const reference๋ก๋ง ๋ฐ์์ผ ํ๋๊ฐ์ ๋ํด์๋ ์๋ ๋งํฌ์ ์ค๋ช ๋์ด์์ต๋๋ค.https://stackoverflow.com/questions/36102728/why-is-it-allowed-to-pass-r-values-by-const-reference-but-not-by-normal-referenc?newreg=af09de2f1d8d479ca830d2af2a5cb732ํ ๊ฐ์ง ์์๊ฐ ๋์ค๋๋ฐvoid display(int& a) { a = 10 };display(1);๊ฐ ์๋ค๋ฉด ๋ง ๊ทธ๋๋ก a = 10์ rvalue์ rvalue๋ฅผ ๋ฃ์ผ๋ผ๋ ๋ง์ ๋๋ค. ๋ง์ด ์๋์ฃ .๋ฐ๋ผ์ ์ง๋ฌธ์๋์ด ๋ง์ํ์ reference๋ง ์ฌ์ฉํ ์ ์์ต๋๋ค๊ทธ๋ผ ์ const๊ฐ ์ด ์ญํ ์ ํ๋๊ฐ๋ c++์ feature๋ก ์ธํ๊ฒ ๊ฐ์ต๋๋ค.Q1: Is the following code legal C++?// Example 1string f() { return "abc"; }void g() {const string& s = f(); cout }A1: Yes.This is a C++ featureโฆ the code is valid and does exactly what it appears to do.Normally, a temporary object lasts only until the end of the full expression in which it appears. However, C++ deliberately specifies that binding a temporary object to a reference to const on the stack lengthens the lifetime of the temporary to the lifetime of the reference itself, and thus avoids what would otherwise be a common dangling-reference error. In the example above, the temporary returned by f() lives until the closing curly brace. (Note this only applies to stack-based references. It doesnโt work for references that are members of objects.)Does this work in practice? Yes, it works on all compilers I tried (except Digital Mars 8.50, so I sent a bug report to Walter to rattle his cage :-) and he quickly fixed it for the Digital Mars 8.51.0 beta).Q2: What if we take out the constโฆ is Example 2 still legal C++?// Example 2string f() { return "abc"; }void g() {string& s = f(); // still legal? cout }A2: No.The "const" is important. The first line is an error and the code wonโt compile portably with this reference to non-const, because f() returns a temporary object (i.e., rvalue) and only lvalues can be bound to references to non-const.์์ฝํ์๋ฉด const reference๋ temporary object(rvalue)์ ์๋ช ์ ์ฐ์ฅ์์ผ ์ค๋ค๊ณ ํฉ๋๋ค. reference๋ก๋ง ๋ฐ์์ ๋๋ ๊ทธ ์๋ช ์ด ํด๋น expression์๋ง ๋จธ๋ฌผ๋ฌ ๋ฌธ์ ๊ฐ ๋ฐ์ํฉ๋๋ค.๋๋ถ์ ์ ๋ ์ฐพ์๋ณด๋ฉด์ ๋ง์ ๊ณต๋ถ๊ฐ ๋์์ต๋๋ค!ํน์ ์๋ชป๋ ๋ด์ฉ์์ผ๋ฉด ๋ต๋ณ์ผ๋ก ์์ ๋ถํ๋๋ฆฝ๋๋ค.
- 0
- 5
- 325
Q&A
์์ฑ์ ์ฐ์ ์์์ ๊ดํ์ง๋ฌธ
์๋ ํ์ธ์! ์ ๋ 1๋ฒ ์ง๋ฌธ๊ณผ ๋น์ทํ ์๋ฌธ์ด ๋ค์ด ๋๋ฒ๊ฑฐ๋ฅผ ์ฐ์ด๋ดค๋๋ฐ์!์์๋์ด ๋ง์ํ์ ๊ฑฐ๋ ์กฐ๊ธ ๋ค๋ฅธ๊ฑฐ ๊ฐ์ ๋ต๋ณ ๋จ๊น๋๋ค!(์์๋์ ์ ์ฑ์ค๋ฌ์ด ๋ต๋ณ์ผ๋ก ํญ์ ๋ง์ด ๋ฐฐ์ฐ๊ณ ์์ต๋๋คใ ใ )#include #include #include using namespace std;class B{private: int m_b;public: B(const int& m_b_in) : m_b(m_b_in) {}};class Something{ // ์์ฑ์๊ฐ ์์์ ์ฌ๊ธฐ์ init // ์์ผ๋ฉด ๊ฑฐ์น์ง ์์private: int m_i = 100; double m_d = 100.0; char m_c = 'F'; int m_array[5] = {100, 200, 300, 400, 500}; B m_b{ 1024 }; public: Something() : m_i(1) , m_d(3.14) , m_c('a') , m_array{1, 2, 3, 4, 5} , m_b(m_i - 1) { m_i = 2; m_d = 6.28; m_c = 'b'; } // ์์๋ ์์์ ์๋๋ก. void print() { cout for (auto& element : m_array) cout cout }};int main(){ Something smth; smth.print(); return 0;}์ ์ ๊ฒฝ์ฐ Something ์์ฑ์ m_i(1)์ ๋๋ฒ๊ฑฐ๊ฐ ์์นํด ์์ ๋ m_i์ ์ฐ๋ ๊ธฐ ๊ฐ์ด ๋ค์ด์์์ต๋๋ค!์ด๋ก ์ถ์ ํด ๋ณธ ๋ฐ, ๋ฉค๋ฒ(private๋ถ๋ถ) ์ด๊ธฐํ๋ฅผ ๊ฑฐ์น์ง ์๊ณ ์์ฑ์์ ๋ค์ด์์ ๋ ์ด๊ธฐํ๊ฐ ๋๋ ๊ฒ์ผ๋ก ์๊ฐํ๋ ๊ฒ์ด ๋ง๋ค๊ณ ์๊ฐํ์ต๋๋ค. ์ถ๊ฐ๋ก ๊ถ๊ธํด์ง ์ฌํญ์ '๊ทธ๋ผ ์์ฑ์ ์ด๋์ ๋ผ์ด์ ธ๋ฆฌ์คํธ ๋ค์ ์ค๊ดํธ ์์ ๋ด์ฉ์ ์ด๊ธฐํ์ธ ๊ฒ์ธ๊ฐ?'๋ผ๋ ์๋ฌธ์ด ๋ค์๋๋ฐ์. ๊ต์๋๊ป์ ์ง์ ๋ณด์ฌ์ฃผ์ จ๋ฏ์ด { m_i *= 2; m_d *= 2.0;}์ ๊ฐ์ ์ฐ์ ์ฐ์ฐ์ด ๊ฐ๋ฅํ ๊ฒ์ ๋ณด์ ์ด๊ธฐํ๊ฐ ์๋ ๊ฒ์ผ๋ก ์ถ์ ๋ฉ๋๋ค. ํน์ ์ ์๊ฐ์ด ์๋ชป๋์๋ค ํ์๋ฉด ๋ต๋ณ์ผ๋ก ์์ ๋ถํ๋๋ฆฝ๋๋ค!
- 0
- 2
- 383
Q&A
extern ์ ์ธ
์ ๋ ์ด์ ์ด ๊ถ๊ธํด์ stackoverflow๋ฅผ ์ฐพ์๋ดค์ต๋๋ค!https://stackoverflow.com/questions/2190919/mixing-extern-and-constUm, since const s are implicitly static, you need an extern even on the a_global_var definition (in file.c). Without this, anything that includes file.h will not link because it is looking for a const int a_global_var with external๋ผ๋ ๋ต๋ณ์ด ์์๋๋ฐ ์ ๊ฐ ์ดํดํ ๋ฐ๋ฅผ ์ค๋ช ๋๋ฆฌ๊ฒ ์ต๋๋ค!์ ๊ธ์์ const ํค์๋๊ฐ ์๋ฌต์ ์ผ๋ก static ์ผ๋ก ์ธ์๋ผ์ const๋ฅผ ๊ฐ์ด ์ฌ์ฉํ ๋๋ extern์ ์ฌ์ฉํ์ฌ ๋ช ์์ ์ผ๋ก ์ ์ญ๋ณ์์์ ๋ํ๋ด ์ฃผ์ด์ผ ํ๋๊ฑฐ ๊ฐ์ต๋๋ค..! ๊ทธ๋์ ๋ง์ํ์ cppํ์ผ์์ header๋ฅผ ๋นผ๋ฉด extern์ด ์์ ๊ฒฝ์ฐ const ํฌํจ ๋ณ์๋ static์ด๊ณ ๋ฐ๋ผ์ main.cpp ๋ฑ ๋ค๋ฅธ cppํ์ผ์์๋ ์ ์ ์๋ ์ํ๊ฐ ๋ฉ๋๋ค. ๊ฒฐ๋ก ์ ์ผ๋ก const๋ฅผ ํตํด ์ ์ํ์ค ๋๋ extern์ ํตํด ๋ช ์ํด์ฃผ๋ ๊ฒ์ด ํ์ํฉ๋๋ค>!ํน์ ํ๋ฆฐ ๋ด์ฉ์ด ์์ผ๋ฉด ๋๊ธ๋ก ์ ์ ๋ถํ๋๋ฆฝ๋๋คใ
- 1
- 2
- 363




