C++:为函数的下一次调用保存变量值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16640867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++: save variable value for next call of the function
提问by speedyTeh
Is there a way to initialize a variable in a function and save its value for next call of function?
有没有办法在函数中初始化变量并保存其值以供下次调用函数时使用?
I'm making application in qt and i have one function connected with a signal. I want an variable in that function to change after the other one reaches its goal. Here is the body of that function:
我正在 qt 中创建应用程序,并且我有一个与信号相关的函数。我希望该函数中的一个变量在另一个函数达到其目标后发生变化。这是该函数的主体:
void objekt::advance(int phase)
{
if(!phase) return;
QPointF location = this->pos();
if (int(location.x())==200 || int(location.x())==-200)
{
smijer=-smijer;
}
setPos(mapToParent(smijer,0));
}
I defined the smijer variable as static int. But i dont'know how to initialize it only once, when program starts, and how to keep its new value after each call of the function.
我将 smijer 变量定义为静态整数。但我不知道如何仅在程序启动时将其初始化一次,以及如何在每次调用该函数后保持其新值。
回答by mecid
Your answer is in your question basically. Static variables (either a class member or local variable of a function) is initialized only once where it is terminated. For example;
你的答案基本上在你的问题中。静态变量(函数的类成员或局部变量)仅在其终止时初始化一次。例如;
#include <iostream>
int foo () {
static int sVar = 5;
sVar++;
return sVar;
}
using namespace std;
int main () {
int iter = 0;
do {
cout << "Svar :" foo() << endl;
iter++;
}while (iter < 3);
}
if you write a program like that it will print out Svar values just like;
如果你写一个这样的程序,它会像这样打印出 Svar 值;
Svar :6
Svar :7
Svar :8
So as you see although we call foo function three times the initialization of a static varible is done only once.
因此,正如您所看到的,尽管我们将 foo 函数调用了三次,但静态变量的初始化只完成了一次。
回答by Matt Phillips
If smijer
is a member of class objekt
, then do it like this:
如果smijer
是 class 的成员objekt
,那么这样做:
objekt.h:
对象.h:
class objekt
{
...
static int smijer;
...
};
objekt.cpp
对象文件
int objekt::smijer = YOUR_INITIALIZATION_VALUE;
On the other hand, if you want/need smijer
to be a global variable, then do it like this:
另一方面,如果你想/需要smijer
成为一个全局变量,那么这样做:
globals.h:
globals.h:
extern int smijer;
globals.cpp //Or another .cpp file
globals.cpp //或另一个.cpp文件
int smijer = YOUR_INITIALIZATION_VALUE;
Although in this case I'd stick it in a namespace. In this case it isn't declared static but it does have the lifetime of your program.
虽然在这种情况下我会将它放在一个命名空间中。在这种情况下,它没有声明为静态的,但它确实具有程序的生命周期。
回答by GRAYgoose124
Why am I being downvoted? He wants to change a variable and preserve the states after function calls. (He doesn't specify whether the variable is a member of the class or anything, so I'm assuming it's not. I'll change my answer if he clarifies and states his question less ambiguously.)
为什么我被低估了?他想改变一个变量并在函数调用后保留状态。(他没有指定变量是类的成员还是其他任何东西,所以我假设它不是。如果他澄清并不那么含糊地陈述他的问题,我会改变我的答案。)
You're going about this wrong. To keep a variable after a function's scope ends, you have to allocate it on the heap rather than the stack. You can use new
or malloc
to do this, but you also have to free this memory with delete
and free
, in that order.
你这样做是错误的。要在函数作用域结束后保留变量,您必须在堆上而不是在堆栈上分配它。您可以使用new
或malloc
来执行此操作,但您还必须按顺序使用delete
和释放此内存free
。
With new
and delete
:
随着new
和delete
:
#include <iostream>
void modify(int * p){
(*p)++;
}
int main(void){
int * pointer = new int;
*pointer = 5;
std::cout << *pointer << std::endl;
modify(pointer);
std::cout << *pointer << std::endl;
delete pointer;
return 0;
}
And with malloc
and free
:
并与malloc
和free
:
#include <iostream>
#include <cstdlib>
void modify(int * p){
(*p)++;
}
int main(void){
int * pointer = (int*)malloc(sizeof(int)); //DO NOT CAST IN C
*pointer = 5;
std::cout << *pointer << std::endl;
modify(pointer);
std::cout << *pointer << std::endl;
free(pointer);
return 0;
}
new
does provide facilities for deleting arrays quickly and is better overall for normal use C++.
new
确实提供了快速删除数组的工具,并且对于正常使用 C++ 来说总体上更好。
回答by Moberg
Declare the variable as static inside the function and the valued will be remembered. You don't need to initialize it. But you can if you want to.
在函数内部将变量声明为静态变量,其值将被记住。你不需要初始化它。但如果你愿意,你可以。