<div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;">아래의 소스는 원래call by reference라면<span style="line-height:21.6000003814697px;font-size:9pt;"> Person(const Person& i) 인데요</span></div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;"><span style="line-height:21.6000003814697px;font-size:9pt;">근데 call by value로 나타낸다면 아래의 소스처럼 된다는데요.</span></div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;"><span style="line-height:21.6000003814697px;font-size:9pt;">컴파일하면 </span><span style="line-height:21.6000003814697px;font-size:9pt;">어차피 컴파일에러가 나지만 만약에 제대로 컴파일이 된다면 </span><span style="line-height:21.6000003814697px;font-size:9pt;">Person obj3(obj2); 문장을 실행시 </span><span style="line-height:21.6000003814697px;font-size:9pt;">무한루프에 빠진다고 써 있어서 한번 오늘 강사님한테 물어보고, 혼자서 생각도 해봤는데요</span></div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;"><span style="line-height:21.6000003814697px;font-size:9pt;"><br></span></div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;"><span style="line-height:21.6000003814697px;font-size:9pt;">처음에 </span><span style="line-height:21.6000003814697px;font-size:9pt;"> Person obj3(obj2);를 실행하면 </span><span style="line-height:21.6000003814697px;font-size:9pt;">Person형의 obj3객체가 생성되면서 생성자 함수</span><span style="font-size:9pt;line-height:21.6000003814697px;">Person(const Person i)에 obj2가 매개변수로 대입되는데 이때 call by value에 의해서 obj2가 i로 메모리에 복사 되고, 또 이것은 </span><span style="line-height:21.6000003814697px;font-size:9pt;">Person obj3(i) 다시 말하면 </span><span style="font-size:9pt;line-height:21.6000003814697px;">i를 생성자의 매개변수로 하는 Person형의 obj3객체가 되고,이러면 </span><span style="font-size:9pt;line-height:21.6000003814697px;">Person(const Person i)와 </span><span style="line-height:21.6000003814697px;font-size:9pt;">형태가 같아지기 때문에 생성자 함수</span><span style="line-height:21.6000003814697px;font-size:9pt;">를 호출하게 되어서, </span><span style="line-height:21.6000003814697px;font-size:9pt;">call by value에 의해서 </span><span style="line-height:21.6000003814697px;font-size:9pt;">또 i가 생성자의 매개변수로 복사되고, 대입되어서,</span><span style="line-height:21.6000003814697px;font-size:9pt;"> </span><span style="line-height:21.6000003814697px;font-size:9pt;">Person obj3(i) 가 되고, 또 </span><span style="line-height:21.6000003814697px;font-size:9pt;">생성자 함수를 호출해서 </span><span style="line-height:21.6000003814697px;font-size:9pt;">또 i가 매개변수로 대입되고 </span><span style="line-height:21.6000003814697px;font-size:9pt;"> </span><span style="line-height:21.6000003814697px;font-size:9pt;">Person obj3(i) 가 되고 </span><span style="line-height:21.6000003814697px;font-size:9pt;">call by value에 의해서...</span><span style="line-height:21.6000003814697px;font-size:9pt;">생성자 함수를 호출해서...</span><span style="font-size:9pt;line-height:21.6000003814697px;">무한히 반복되는 것인거 같은데요.</span></div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;"><span style="font-size:9pt;line-height:21.6000003814697px;"><br></span></div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;"><span style="font-size:9pt;line-height:21.6000003814697px;">생각해보면 이렇게 실행된다면 절대 </span><span style="line-height:21.6000003814697px;font-size:9pt;">cout<<"Person(const Person i)호출"<<endl;이 문장이 실행되지 않고 계속 call by value에 의해서 생성자의 매개변수가 메모리에 복사되고 그후 생성자 함수의 매개변수에 대입되고 매개변수가 복사되고 대입되고만 영원히 반복되는것 같은데 만약에 이 call by value에 의한 무한루프가 실행된다면 </span><span style="line-height:21.6000003814697px;font-size:9pt;">Person(const Person i)호출 메세지가 정말로 나오지 않는지 알고 싶네요.</span></div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;"><span style="line-height:21.6000003814697px;font-size:9pt;">그리고 제 생각이 틀렸다면 어떤 흐름으로 무한루프가 되는지 알려 주신다면 정말 좋겠네요.</span></div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;"><br></div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;"><br></div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;">#include <iostream></div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;">using std::cout;</div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;">using std::endl;</div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;">using std::cin;</div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;"><br></div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;">class Person</div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;">{</div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;">public:</div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;"> Person(){</div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;"> cout<<"Person()호출"<<endl;</div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;"> }</div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;"> Person(int i){</div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;"> cout<<"Person(int i)호출"<<endl;</div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;"> }</div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;"> Person(const Person i)</div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;"> {</div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;"> cout<<"Person(const Person i)호출"<<endl;</div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;"> }</div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;">};</div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;"><br></div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;">int main()</div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;">{</div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;"> Person obj1;</div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;"> Person obj2(10);</div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;"> Person obj3(obj2);</div> <div style="font-family:'굴림', gulim, '돋움', Dotum, Helvetica, AppleGothic, sans-serif;line-height:21.6000003814697px;">}</div> <div><br></div>
댓글 분란 또는 분쟁 때문에 전체 댓글이 블라인드 처리되었습니다.