Programing

십자가 초기화의 징후는 무엇입니까?

lottogame 2020. 10. 8. 07:39
반응형

십자가 초기화의 징후는 무엇입니까?


다음 코드를 고려하십시오.

#include <iostream>
using namespace std;

int main()
{
    int x, y, i;
    cin >> x >> y >> i;
    switch(i) {
        case 1:
            // int r = x + y; -- OK
            int r = 1; // Failed to Compile
            cout << r;
            break;
        case 2:
            r = x - y;
            cout << r;
            break;
    };
}

G ++ crosses initialization of 'int r'는. 제 질문은 다음과 같습니다.

  1. 무엇입니까 crosses initialization?
  2. 첫 번째 이니셜 라이저 x + y가 컴파일을 통과했지만 나중에 실패한 이유는 무엇 입니까?
  3. 소위 문제는 무엇입니까 crosses initialization?

편집 :
범위를 지정하기 위해 대괄호를 사용해야한다는 것을 알고 r있지만 왜 비 -POD가 다중 케이스 스위치 문에서 정의 될 수 없는지 알고 싶습니다.

감사.


버전도 int r = x + y;컴파일되지 않습니다.

문제는 r이니셜 라이저가 실행되지 않고 범위에 올 수 있다는 것입니다 . 이니셜 라이저를 완전히 제거하면 코드가 잘 컴파일됩니다 int r;.

가장 좋은 방법은 변수의 범위를 제한하는 것입니다. 그렇게하면 컴파일러와 독자 모두를 만족시킬 수 있습니다.

switch(i)
{
case 1:
    {
        int r = 1;
        cout << r;
    }
    break;
case 2:
    {
        int r = x - y;
        cout << r;
    }
    break;
};

표준에 따르면 (6.7 / 3) :

블록으로 전송할 수 있지만 초기화를 통해 선언을 우회하는 방식은 아닙니다. 자동 저장 기간이있는 지역 변수가 범위 내에 있지 않은 지점에서 범위 내에있는 지점으로 점프하는 프로그램은 변수에 POD 유형 (3.9)이 있고 이니셜 라이저 (8.5)없이 선언되지 않는 한 잘못된 형식입니다.


case범위를 지정하려면 괄호 안에 의 내용을 넣어야합니다. 그러면 그 안에 지역 변수를 선언 할 수 있습니다.

switch(i) {
    case 1:
        {
            // int r = x + y; -- OK
            int r = 1; // Failed to Compile
            cout << r;
        }
        break;
    case 2:
        ...
        break;
};

It is possible to transfer into a block, but not in a way that bypasses declarations with initialization. A program that jumps from a point where a local variable with automatic storage duration is not in scope to a point where it is in scope is ill-formed unless the variable has POD type and is declared without an initializer.

[Example: Code:

void f()
{
  // ...
  goto lx;    // ill-formed: jump into scope of `a'
  // ...
 ly:
    X a = 1;
  // ...
 lx:
   goto ly;    // ok, jump implies destructor
 // call for `a' followed by construction
 // again immediately following label ly
}

--end example]

The transfer from the condition of a switch statement to a case label is considered a jump in this respect.


I suggest you promote your r variable before the switch statement. If you want to use a variable across the case blocks, (or the same variable name but different usages), define it before the switch statement:

#include <iostream>
using namespace std;

int main()
{
    int x, y, i;
    cin >> x >> y >> i;
// Define the variable before the switch.
    int r;
    switch(i) {
        case 1:
            r = x + y
            cout << r;
            break;
        case 2:
            r = x - y;
            cout << r;
            break;
    };
}

One of the benefits is that the compiler does not have to perform local allocation (a.k.a. pushing onto the stack) in each case block.

A drawback to this approach is when cases "fall" into other cases (i.e. without using break), as the variable will have a previous value.

참고URL : https://stackoverflow.com/questions/2392655/what-are-the-signs-of-crosses-initialization

반응형