我尝试通过在函数中声明它来打印静态变量的值 5 次,在该函数中每次调用都会增加自身,然后将其添加到全局变量并在 printf 语句中返回其值,但输出与通常的所有静态值不同变量先递增,添加到全局变量后输出顺序相反(所有值都使用单个 printf 语句打印)
#include <stdio.h>
int global_variable = 10;
int fun(){
static int var;
printf("The value of var is %d\n", var);
var++;
return global_variable + var;
}
int main()
{
//This works fine
printf("%d\n", fun());
printf("%d\n", fun());
printf("%d\n", fun());
printf("%d\n", fun());
printf("%d\n", fun());
//This works weird this prints value in reverse order not like the former case
printf("\n%d\n%d\n%d\n%d\n%d\n",fun(), fun(), fun(), fun(), fun());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
第一个输出:
The value of var is 0
11
The value of …Run Code Online (Sandbox Code Playgroud) 我有一些使用静态变量的C#库类.我将这些库类用于桌面和Web应用程序.问题是,正如我刚刚发现的那样,静态变量在Web服务器上不会那么好; 使用网站在所有会话中共享这些值!
如何保留静态变量的功能以便在我的桌面应用程序中使用,同时确保我的Web服务器上的每个会话都有自己的这些变量的独立值 - 但在会话本身内,它仍然真的像静态一样?
我在WinForms应用程序中有两种不同的形式(MainForm和Form2说).他们都通过"getInstance"静态方法请求访问MyDataSet.问题是在MainForm有一个实例之后,当Form2需要获取一个实例静态"myDataSet"变量为null时,我希望已经设置了?有任何想法吗?
public class MyDataSet
{
public static MyDataSet myDataSet;
// This was null 2nd call to getInstance
private DataSet myData = new DataSet();
public static MyDataSet GetInstance()
{
if (myDataSet == null)
{
return new MyDataSet();
}
else
{
return myDataSet;
}
}
Run Code Online (Sandbox Code Playgroud)
所以几乎看起来静态的"myDataSet"变量不仅仅有一次实例吗?
我有一个PHP函数seperate php file,我from another php file通过使用一个调用此函数jquery ajax call.php函数只是将其静态值增加1,但是当我看到输出时它没有递增.静态变量不像我想的那样表现.
是什么原因?
提前致谢,
功能简单:
function IncrementByOne()
{
static $count = 0;
$count++;
echo $count;
}
Run Code Online (Sandbox Code Playgroud) 我有一个bankAccount对象,我想使用构造函数递增.目标是让它与类实例化的每个新对象一起递增.
注意:我重写了ToString()以显示accountType和accountNumber;
这是我的代码:
public class SavingsAccount
{
private static int accountNumber = 1000;
private bool active;
private decimal balance;
public SavingsAccount(bool active, decimal balance, string accountType)
{
accountNumber++;
this.active = active;
this.balance = balance;
this.accountType = accountType;
}
}
Run Code Online (Sandbox Code Playgroud)
为什么当我将其插入主体时如此:
class Program
{
static void Main(string[] args)
{
SavingsAccount potato = new SavingsAccount(true, 100.0m, "Savings");
SavingsAccount magician = new SavingsAccount(true, 200.0m, "Savings");
Console.WriteLine(potato.ToString());
Console.WriteLine(magician.ToString());
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出不会单独递增,即
savings 1001
savings 1002
Run Code Online (Sandbox Code Playgroud)
但相反,我得到:
savings 1002
savings 1002
Run Code Online (Sandbox Code Playgroud)
我如何使它成为前者而不是后者?
我有一个模板化的类叫PooledResource.它为我处理像纹理和声音这样的事情,并确保我不会在事故中多次加载同一个.某种类型的资源将始终位于某个子目录中,我希望能够更轻松地加载它们,因此我决定添加一个静态变量来跟踪某种类型资源的根文件夹.
template <typename T>
class PooledResource
{
public:
// ...
static const char* PathPrefix;
static ResourceTable myResourceTable;
static void load(const char* filename)
{
// Any attempt to use PathPrefix in here causes a compilation error
// All I want to do is use PathPrefix and filename to get the full
// path do a file and load it.
}
};
template <typename T>
const char* PooledResource<T>::PathPrefix = NULL;
template <typename T>
typename PooledResource<T>::ResourceTable PooledResource<T>::myResourceTable;
Run Code Online (Sandbox Code Playgroud)
我的想法是我可以为各种类型使用typedef'd版本.例如,PooledTexture.hpp我会:
typedef …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
#include <stdio.h>
int f1()
{
static int s=10;
printf("s=%d\n",s++);
}
int main()
{
f1();
f1();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
s=10
s=11
Run Code Online (Sandbox Code Playgroud)
为什么static int s=10第二次忽略该行,何时f1被调用?
以下代码在Windows或Linux(Debian)上运行良好,但在Mac上报告退出时崩溃:
int main(int argc, char *argv[]) {
static QApplication app(argc, argv);
QPushButton w; w.show();
return app.exec();
}
Run Code Online (Sandbox Code Playgroud)
我需要这个app变量必须是静态的.我解决这个问题:
static QApplication& app = *new QApplication(argc, argv);
Run Code Online (Sandbox Code Playgroud)
但我不喜欢这种风格.你有什么建议吗?非常感谢你!
内部函数静态变量在再次调用时保留其值,但为什么在这种情况下不会发生?
#include <stdio.h>
void print(void) {
static int x;
x = 10;
x += 5;
printf("%d ", x);
}
int main() {
print();
print();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它输出为15 15.
我正在尝试学习在给定函数中定义静态变量时在c中如何工作。例如,当我编写以下代码时:
#include <stdio.h>
void inc() {
static int c = 0;
c++;
printf("%d\n", c);
}
int main(void) {
inc();
inc();
inc();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
预期的输出显然是:
1
2
3
Run Code Online (Sandbox Code Playgroud)
在第一次调用该函数时,将定义静态变量c并将其值设为0,这是很合理的。它已递增并打印。但是,在第二次询问inc()为什么要保留整数c而不将其设置为零的情况下,即使代码按字面意思说也是如此static int c = 0;。像第一次调用期间一样,编译器中的哪种机制阻止c将其值设置为零?