for 또는 while없이 생성자의 무한 루프
여기서 테스트를했지만 출력이 끝나지 않는 루프입니다. 이유를 모르겠습니다.
사실 저는 다른 테스트를하고 있는데 이것을 작성했을 때 루프가 어떻게 발생했는지 이해할 수 없습니다. 반복해서 "ABC"를 출력합니다.
#include <map>
#include <string>
#include <iostream>
class test
{
public:
std::map <int, int> _b;
test();
test (std::map<int, int> & im);
~test();
};
test::test()
{
std::cout<<"abc";
_b.clear();
_b[1]=1;
test(_b);
}
test::test(std::map <int, int>& im)
{
std::cout<<im[1];
}
test::~test() {};
int main ()
{
test a;
}
여기서 문제는 컴파일러가
test(_b);
test
parameter를 전달하는 형식의 임시 개체를 만드는 코드가 _b
아니라 기본 생성자를 사용하여 _b
type 이라는 변수에 대한 변수 선언으로 test
사용됩니다. 결과적으로 test
두 번째 생성자를 사용하여 임시 객체를 만드는 코드 조각처럼 보이는 것은 대신 재귀 적으로 유형의 새 객체를 만들고 test
생성자를 다시 호출하는 것입니다.
이 문제를 해결하기 위해 변수에 다음과 같은 명시적인 이름을 지정할 수 있습니다.
test t(_b);
이것은 두 번째 생성자를 사용하여 초기화 된 test
라는 유형의 변수로만 해석 될 수 있습니다 t
.
나는 이것을 전에 본 적이 없으며 수년 동안 C ++로 프로그래밍했습니다. 언어의 또 다른 코너 케이스 를 보여 주셔서 감사 합니다!
공식적인 설명 : C ++ 03 ISO 사양, §6.8에 따르면 :
표현식 문과 선언을 포함하는 문법에 모호성이 있습니다. 함수 스타일 명시 적 유형 변환 (5.2.3)을 사용하는 표현식 문은 맨 왼쪽 하위 표현식으로 첫 번째 선언자가 (.txt)로 시작하는 선언과 구별 할 수 없습니다. 이 경우 진술은 선언입니다.
(내 강조). 즉, C ++가 문을 표현식 (임시 객체 캐스트) 또는 선언 (변수)으로 해석 할 수있을 때마다 선언을 선택합니다. C ++ 사양은 명시 적으로
고마워);
선언의 예로서 a
유형 의 캐스트가 아닙니다 T
.
이것은 C ++의 가장 까다로운 구문 분석입니다 . 표현식처럼 보이는 것이 대신 선언으로 해석됩니다. 전에 MVP를 본 적이 있지만 이런 맥락에서 본 적이 없습니다.
도움이 되었기를 바랍니다!
문제는 생성자에서 다시 생성자 test (_b)를 호출하는 것입니다.
test :: test () {std :: cout << "abc"; _ b.clear (); _ b [1] = 1; test (_b);}
여기에 무슨 일이 일어나는가
everytime you call test(_b) it first calls default constructor test::test and it in turns calls the test(_b) and the loop goes on and on untill the stack overflows.
remove the test(_b) from the default constructor
I'm pretty sure that you are not actually "calling the constructor" since they are not directly callable IIRC. The legalese had to do with constructors not being named functions - I don't have a copy of the Standard handy or I might quote it. I believe what you are doing with test(_b)
is creating an unnamed a temporary which invokes the default constructor again.
I'm not familiar with the particularities of the standard, but it may be that calling a constructor within a constructor is undefined. As such it could be compiler dependent. In this particular case it causes infinite recursion of your default constructor without ever calling your constructor with the map argument.
C++ FAQ 10.3 has an example with a constructor that has two parameters. If you add an int parameters to your second constructor such as test(map, int)
, it exhibits a somewhat normal behaviour.
For good form I would simply change test::test(std::map <int, int>& im)
for test::testInit(std::map <int, int>& im)
, and test(_b)
to testInit(_b)
.
참고URL : https://stackoverflow.com/questions/16200933/infinite-loop-in-constructor-without-for-or-while
'Program Tip' 카테고리의 다른 글
현재 준비되었거나 커밋 된 git에서 무시한 파일을 나열하는 방법은 무엇입니까? (0) | 2020.12.01 |
---|---|
httptest를 사용하여 Go에서 http 호출을 테스트하는 방법 (0) | 2020.12.01 |
localstorage (또는 다른 곳)에서 ES6 맵을 유지하려면 어떻게해야합니까? (0) | 2020.12.01 |
pylint logging-not-lazy를 수정하는 방법은 무엇입니까? (0) | 2020.12.01 |
JavaScript로 CSS를 추가 하시겠습니까? (0) | 2020.12.01 |