2017-05-12 5 views
-1

次のプログラムでは、main()関数内でクラスを宣言しました。C++のmain()関数内で宣言されたクラス

ケース1:

int main() 
{ 
     static int i = 10; // static variable 

     class A 
     { 
     public: 
       A() 
       { 
        std::cout<<i; 
       } 
     }; 
     A a; 
     return 0; 
} 

とそれはG ++コンパイラで正常に動作しています。

しかし、staticキーワードを削除してコンパイルすると、コンパイラでエラーが発生します。

ケース2:

int main() 
{ 
     int i = 10; // removed static keyword 

     class A 
     { 
     public: 
       A() 
       { 
        std::cout<<i; 
       } 
     }; 
     A a; 
     return 0; 
} 

エラー:

In constructor 'main()::A::A()': 
13:32: error: use of local variable with automatic storage from containing function 
:cout<<i; 
           ^
7:13: note: 'int i' declared here 
     int i = 10; 
      ^

ケース1が正常に動作し、なぜ?なぜケース2を動作させないのですか?

+4

あなたの質問はありますか? – Jarod42

答えて

4

なぜ機能しないのですか? https://www.quora.com/Why-cant-local-class-access-non-static-variables-of-enclosing-function

You are wondering about a variable outside of a class. I will explain this the none-C++ way. Let's look at it from the paradigm of general machine architecture and the way programming languages are oft defined. The issue is stack frames, the concept of the stack, and how program refer to memory locations.

When a function is called, the variables of that function are pushed onto the stack. A function and its variables are often a sequence of memory locations. When the function is finished, it and those variables are popped off the stack. That means when the function is called, variables come into existence. When the function is done, the variables depart immediately. Each variable, like the function itself are memory locations (may be assigned to registers).

Declaring the class does not declare a variable. The class is just a definition in the world of C++ and has no linkage to the variable defined in the outer scope. The phrase, automatic storage duration, is roughly synonymous with the idea of the variable (memory) automatically recovered when the function exits. Even though it is C++, when it compiles, it is still machine language and will obey the rules of the machine. The method you called on the class is part of the class but is not part of the function. Only the class definition is local to the function.

All functions, regardless of where they exist are their own stack frame. The standard way stack frames are done means, unless the memory location referenced is valid, the data will be inaccessible by the time the function in the class is called. In this case, it isn't because the variable in the outer scope has been reclaimed, but because when the method in the class is called, the stack frame in which the outer variable exists is not active in the series of registers used by the method being called. The compiler was encoded with the understanding of this process and gave an error message to ward off the trouble that would ensue if such access was attempted.

You can still access the variable if you affix the static keyword to the variable. That is mentioned in the web page Local Classes in C++ that has the same code example you have listed. Basically, you have to extend the storage duration or the duration that the memory for the variable remains valid in the enclosing scope. Generally, a good way to think through these kind of error messages is through knowledge of the language specification, but in terms of time, relating the representations back to machine architecture can zero in on the underlying reasons why.

から

のコピー/ペーストはどのようにそれを回避するには?

クラスの内部で使用したい変数をコンストラクタの引数として渡します(私は参照メンバにしましたので、iの変更はクラスの内部でも見えますが、関数が終了するとすぐに、iは範囲外になります)。

#include<iostream> 

int main() 
{ 
    int i = 10; // static variable 

    class A 
    { 
    private: 
    int &r_i; 
    public: 
    A(int &i) 
    : 
     r_i(i) 
    { 
     std::cout<<r_i; 
    } 
    }; 
    A a(i); 
    return 0; 
} 
関連する問題