在C++中的main()函数内声明的类

问题描述:

在下面的程序中,我已经在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

它为什么不起作用?从 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; 
}