gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-27 22:45:11 26 4
gpt4 key购买 nike

在下面的程序中,我在 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 不起作用?

最佳答案

为什么不起作用?

复制/粘贴自 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 explainthis the none-C++ way. Let's look at it from the paradigm of generalmachine architecture and the way programming languages are oftdefined. The issue is stack frames, the concept of the stack, and howprogram refer to memory locations.

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

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

All functions, regardless of where they exist are their own stackframe. The standard way stack frames are done means, unless the memorylocation referenced is valid, the data will be inaccessible by thetime the function in the class is called. In this case, it isn'tbecause the variable in the outer scope has been reclaimed, butbecause when the method in the class is called, the stack frame inwhich the outer variable exists is not active in the series ofregisters used by the method being called. The compiler was encodedwith the understanding of this process and gave an error message toward off the trouble that would ensue if such access was attempted.

You can still access the variable if you affix the static keyword tothe variable. That is mentioned in the web page Local Classes in C++that has the same code example you have listed. Basically, you have toextend the storage duration or the duration that the memory for thevariable remains valid in the enclosing scope. Generally, a good wayto think through these kind of error messages is through knowledge ofthe language specification, but in terms of time, relating therepresentations back to machine architecture can zero in on theunderlying 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;
}

关于c++ - 在 C++ 的 main() 函数中声明的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43932288/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com