我已经多次遇到这个问题而且我从不费心去了解它为什么会发生并学习"静态"实际意味着什么.我刚刚应用了Eclipse建议并继续进行的更改.
public class Member {
// Global Variables
int iNumVertices;
int iNumEdges;
public static void main(String[] args) {
// do stuff
iNumVertices = 0; // Cannot make a static reference to the non-static field iNumVertices
// do more stuff
} // main end
}
Run Code Online (Sandbox Code Playgroud)
所以eclipse告诉我这样做static int iNumVertices;,我不知道为什么.那究竟什么是"静态",它是如何使用的,使用"静态"的目的是什么,为什么它会给我这个问题呢?
void foo() {
static int x;
}
void bar() {
static int x;
}
int main() {
foo();
bar();
}
Run Code Online (Sandbox Code Playgroud) 请注意,这只是一个思想实验.
我知道全局(静态)变量很糟糕,在任何情况下破坏范围都是一个坏主意.
请考虑以下代码:
function IsItChanged: integer;
const
CanIBeChanged: integer = 0;
begin
Result:= CanIBeChanged;
end;
Run Code Online (Sandbox Code Playgroud)
假设已启用可写常量,如何更改CanIBeChanged其声明的函数范围之外的值?
PS不,我不打算永远使用这个代码,这只是一个有趣的问题.
在我的应用程序中,我有Loginactivity.它有一个静态变量用户名,它将被分配给用户输入用户名的值.Loginactivity启动活动A和A启动B.在A i中使用变量Loginactivity.username.
现在由于B中的一些错误,应用程序崩溃了.当我按下强制关闭时,应用程序重新启动,活动A是当前活动.在活动AI中使用静态变量Loginactivity.username.我看到崩溃后这个变量的初始值为空字符串"";
为什么会这样?你能解释一下这种行为吗?因此,当应用程序崩溃时,堆栈中的所有活动都会重新启动?我看到登录活动的oncreate没有被调用.那么静态变量值如何变化?
请查看此标题:
// Test.h
@interface Test : NSObject @end
extern id A; // (0)
//extern static id B; // (1) Uncomment to get a compiling error
extern id C; // (2)
//extern static id D; // (3) Uncomment to get a compiling error
Run Code Online (Sandbox Code Playgroud)
并进入这个实现:
// Test.m
#import "Test.h"
id A = @"A"; // (4)
static id B = @"B"; // (5)
@implementation Test
id C = @"C"; // (6)
static id D = @"D"; // (7)
@end
// Still Test.m
@interface …Run Code Online (Sandbox Code Playgroud) 注意:我的问题与此无关.
除了在文件外部看不到变量之外,在内存分配方面,声明它(在方法之外)之间是否有任何区别:
NSString *const kMyConstant = @"Hello World";
Run Code Online (Sandbox Code Playgroud)
或这个:
static NSString *const kMyConstant = @"Hello World";
Run Code Online (Sandbox Code Playgroud) 我知道constexpr 变量可以在编译时使用.例如,对于模板或静态asser.
但如果我想在没有constexpr的情况下这样做,我可以static const.
什么是自C++ 11/14引入constexpr之间的区别
constexpr int a = 3;
//AND
static const int a = 3;
Run Code Online (Sandbox Code Playgroud)
谢谢!
另一种看待这个问题的方法是我应该使用哪种方法?
假设我有以下代码
#pragma once
class Something {
public:
static Something& get();
private:
Something();
};
Run Code Online (Sandbox Code Playgroud)
#include "Something.hpp"
#include <iostream>
using namespace std;
Something& Something::get() {
static Something something;
return something;
}
Something::Something() {
cout << "Something()" << endl;
}
Run Code Online (Sandbox Code Playgroud)
#include <iostream>
using namespace std;
struct SomethingElse {
~SomethingElse() {
Something::get();
cout << "~SomethingElse" << endl;
}
};
void func() {
static SomethingElse something_else;
// do something with something_else
}
int main() {
func();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Something是否可以创建多个对象实例?标准是否说明了序列化静态对象的破坏?
注意 …
我对AppDomain中的共享/静态对象生存期很好奇,其中RemotingCalls是创建共享对象的原因.
我们正在使用Remoting设置,它使用客户端激活的对象,我们只使用这些功能进入服务器.远程对象设置为单例.
服务器设置通道并使用RemotingConfiguration.Configure加载配置文件.
其中一些服务器功能触摸并使用服务器上的一些静态(在vb.net中共享)变量.我无法找出这些静态变量的生命周期,它们是在第一次触摸时创建的(静态构造函数运行).使用日志记录我无法看到对象dispose/finalize发生.
连接到远程服务器后等待几分钟,看到共享对象活得很好.
问题:
那么这个远程处理设置中静态对象的预期实时时间是多少.它们是否与AppDomain一样长,或者当Remoting对象被交换时它们会被循环使用.如果需要,延长寿命的正确方法是什么?
答案:
静态类型存在于AppDomain中,因为它们是第一次访问,直到AppDomain被卸载.因此,只要AppDomain正在运行,您就不需要延长其生命周期.
我只是非常奇怪(对我而言)java的行为.我有以下课程:
public abstract class Unit {
public static final Unit KM = KMUnit.INSTANCE;
public static final Unit METERS = MeterUnit.INSTANCE;
protected Unit() {
}
public abstract double getValueInUnit(double value, Unit unit);
protected abstract double getValueInMeters(double value);
}
Run Code Online (Sandbox Code Playgroud)
和:
public class KMUnit extends Unit {
public static final Unit INSTANCE = new KMUnit();
private KMUnit() {
}
//here are abstract methods overriden
}
public class MeterUnit extends Unit {
public static final Unit INSTANCE = new MeterUnit();
private MeterUnit() {
} …Run Code Online (Sandbox Code Playgroud)