为什么有些人这样做:
char baa(int x) {
static char foo[] = " .. ";
return foo[x ..];
}
Run Code Online (Sandbox Code Playgroud)
代替:
char baa(int x) {
char foo[] = " .. ";
return foo[x ..];
}
Run Code Online (Sandbox Code Playgroud)
在linux源代码应用程序上看起来很常见.有性能差异?如果有,有人可以解释原因吗?提前致谢.
在C#中我有一个泛型类:
public class MyGeneric<ParameterClass> where ParameterClass: MyGenericParameterClass, new() {
public static int Variable;
}
Run Code Online (Sandbox Code Playgroud)
现在在C++中如果我实例化一个带有不同参数的模板化类,每个完整的类都会得到它自己的Variable,所以我就是说不出来
MyGeneric.Variable = 1; // invalid in C++
Run Code Online (Sandbox Code Playgroud)
在C++中,但我似乎可以在C#中这样做.
我想澄清一下......
如果我有一个带有静态成员变量的泛型,那么该变量是否在所有泛型实例之间共享?
我想知道我是否可以使用静态变量进行优化:
public function Bar() {
static $i = moderatelyExpensiveFunctionCall();
if ($i) {
return something();
} else {
return somethingElse();
}
}
Run Code Online (Sandbox Code Playgroud)
我知道,一旦$i初始化,它将不会被连续调用的那行代码所改变Bar().我认为这意味着moderatelyExpensiveFunctionCall()每次打电话都不会评估,但我想肯定地知道.
一旦PHP看到一个已初始化的静态变量,它是否会跳过该行代码?换句话说,如果我打了很多电话Bar(),或者我在浪费时间,这会优化我的执行时间吗?
所以我使用的是静态类成员,因此我可以在类方法和同一个类的静态方法之间共享数据(只有1个类的实例化).我理解这很好,但我只是想知道静态成员什么时候被初始化?它是进口的吗?在第一次使用课程?因为我将从多个模块调用此类的静态成员(因此多于1个import语句).访问静态方法的所有模块是否共享相同的静态数据成员?如果我的主客户端删除了我的类的实例,然后重新创建它(没有完全终止或重新导入东西),我的数据成员是否会被保留?
好的,在C,C++,C#和Objective-C之间切换时仍然会重新调整,所以有时我的脑袋会旋转.然而,这一次,我对于正确的方式更加困惑,因为我已经看到至少有三种不同的方式在Objective-C中声明静态变量,如果你认为它只是C本身的超集,那就是第四种.那么这些是对的?
如果我们想要共享一个独立变量(即不是一个静态类变量,而是一个刚刚在头文件中定义的变量),那就像在'C'中那样(在标题中带有'extern'的ala)?
foo.h中
@interface Foo : NSObject{
static int Laa;
}
@end
Run Code Online (Sandbox Code Playgroud)
Foo.m
@implementation Foo
...
@end
Run Code Online (Sandbox Code Playgroud)
foo.h中
@interface Foo : NSObject{
}
@end
Run Code Online (Sandbox Code Playgroud)
Foo.m
static int Laa; // <-- Outside of the implementation
@implementation Foo
...
@end
Run Code Online (Sandbox Code Playgroud)
foo.h中
@interface Foo : NSObject{
}
@end
Run Code Online (Sandbox Code Playgroud)
Foo.m
int Laa; // <-- Note no word 'static' here like in 'Option B'
@implementation Foo
...
@end
Run Code Online (Sandbox Code Playgroud)
foo.h中
static int Laa;
@interface Foo : NSObject{
}
@end
Run Code Online (Sandbox Code Playgroud)
Foo.m
@implementation …Run Code Online (Sandbox Code Playgroud) 假设我有这个程序:
class Foo {
public:
unsigned int bar () {
static unsigned int counter = 0;
return counter++;
}
};
int main ()
{
Foo a;
Foo b;
}
Run Code Online (Sandbox Code Playgroud)
(当然这个例子没有任何意义,因为我显然将"counter"声明为私有属性,但它只是为了说明问题).
我想知道C++在这种情况下的行为:bar()方法中的变量"counter"对于每个实例都是一样的吗?
是否可以使用"块类型"的静态变量?
我有一个只在静态方法中做的东西.执行那些我打电话的方法statusChangedBlock.为此,我创建了一个类的共享实例,并使用其单个块属性.我想知道是否有可能有一个静态块变量; 所以我不必创建一个具有单个属性的实例,只是为了通知我的状态已更改.
我知道有一个NSNotification的选项,但我不喜欢使用它,但有一些罕见的例外.
......这个问题听起来有些愚蠢,我不知道为什么.我希望有人指出这一点.
Will dealloc(下面)释放NSString静态变量指向的exampleString?
// ExampleClass.h
@interface ExampleClass : NSObject
@end
// ExampleClass.m
static NSString *exampleString;
@implementation ExampleClass
- (void)dealloc {
exampleString = nil;
}
- (id)init {
self = [super init];
if (self) {
exampleString = [NSString stringWithFormat:@"example %@", @"format"];
}
return self;
}
@end
Run Code Online (Sandbox Code Playgroud) 我有2个选择:
单身模式
class Singleton{
private static Singleton singleton = null;
public static synchronized Singleton getInstance(){
if(singleton == null){
singleton = new Singleton();
}
return singleton;
}
}
Run Code Online (Sandbox Code Playgroud)使用一个static final字段
private static final Singleton singleton = new Singleton();
public static Singleton getSingleton() {
return singleton;
}
Run Code Online (Sandbox Code Playgroud)有什么不同?(单线程或多线程)
更新:我知道Bill Pugh或enum方法.我不是在寻找正确的方法,但我只使用过1.在1或2中是否有任何差异?
我有三个文件 - global.php, test.php,test1.php
Global.php
$filename;
$filename = "test";
Run Code Online (Sandbox Code Playgroud)
test.php的
$filename = "myfile.jpg";
echo $filename;
Run Code Online (Sandbox Code Playgroud)
test1.php
echo $filename;
我可以通过测试和test1文件读取此变量
include 'global.php';
现在我想设置$filenamein 的值test.php和我想读的相同值test1.php.
我尝试了会话变量,但由于两个不同的文件,我无法捕获变量.
怎么实现这个........
在此先感谢您的帮助.....
static-variables ×10
objective-c ×3
php ×2
static ×2
c ×1
c# ×1
c++ ×1
file-access ×1
generics ×1
java ×1
optimization ×1
python ×1
singleton ×1