static-variables

What is the difference between a static const and constexpr variable?

夙愿已清 提交于 2019-12-05 05:32:49
I understand that a constexpr variable can be used at compiletime. For a template, or static asser for instance. But if I want to do that without constexpr I can with static const . What is since C++11/14 introduced constexpr the difference between constexpr int a = 3; //AND static const int a = 3; Thank you! Another way to see this question is which should I use? The main difference that I know is, the value of constexpr must be known in compile-time while a const static can be assigned in run-time. const static int x = rand(); 来源: https://stackoverflow.com/questions/23538440/what-is-the

Python - Static Variable inside Function

北战南征 提交于 2019-12-04 19:11:51
I am trying to set a static variable inside a function. Essentially, I want this variable to be false initially. After the first time this function is called, I want the variable to be set to true . I currently have the following: class LKTracker(object): def track_points(self,width,height): if not hasattr(track_points, "gotInitialFeatures"): track_points.gotInitialFeatures = None if not track_points.gotInitialFeatures: #do some stuff track_points.gotInitialFeatures = True With this code, I keep receiving the following error: NameError: global name 'track_points' is not defined Anyone know

Initialization of static variables in C [duplicate]

佐手、 提交于 2019-12-04 16:29:47
Possible Duplicate: The initialization of static variable in C I know that either global variables or static are automatically initialized with zero in C. However, I'm not sure if both or only one of them are initialized. Note that I'm not talking about variables defined in functions but globally in the .c file. So which of the following variables are automatically initialized with zero? static struct mystruct var1; struct mystruct var2; static struct { int x; int y; } var3; C FAQ . Asim Ihsan I ran the following code in codepad struct mystruct { int a; }; static struct mystruct var1; struct

How does local() differ from other approaches to closure in R?

♀尐吖头ヾ 提交于 2019-12-04 10:10:12
问题 Yesterday I learned from Bill Venables how local() can help create static functions and variables, e.g., example <- local({ hidden.x <- "You can't see me!" hidden.fn <- function(){ cat("\"hidden.fn()\"") } function(){ cat("You can see and call example()\n") cat("but you can't see hidden.x\n") cat("and you can't call ") hidden.fn() cat("\n") } }) which behaves as follows from the command prompt: > ls() [1] "example" > example() You can see and call example() but you can't see hidden.x and you

What exactly does “static” mean when declaring “global” variables in C++?

我们两清 提交于 2019-12-04 07:20:13
This is an expansion of the scope of a previous question of mine . What exactly is "static", how is it used, and what is the purpose of using "static" when dealing with C++? Thanks. The keyword static has different meanings in C++, depending on the context. When declaring a free function or a global variable it means that the function is not to be available outside of this single translation unit: // test.cpp static int a = 1; static void foo() {} If the result of compiling that translation unit is linked with a different translation unit containing symbols a and foo it will not break the One

C++ static variables initialization order

馋奶兔 提交于 2019-12-04 07:04:41
1) If I'm not mistaken, C++ standard guarantees that static variables in a single translation unit are initialized in their definition order. And I'm confused about the following code fragment: extern int n; int k = n; int n = 2; extern int n; is the declaration, not the definition, so k is defined before n , but GCC, Clang and MSVC all show me that k == 2 after the initialization of the global variables. For me it's unclear how can k be assigned 2 after int k = n; , because n is not initialized at that point yet and its value must be zero. If we change the last line to: int n = func(); where

Static Variable Declaration (C)

孤人 提交于 2019-12-04 04:18:33
Are the following two static variable declarations equivalent? 1. static int var1; static int var2; static int var3; 2. static int var1, var2, var3; More specifically, in case 2, will all variables be static , or just var1 ? Yes the declarations in case 1 and 2 are identical. We can see this by going to the draft C99 standard section 6.7.5 Declarators which says ( emphasis mine going forward ): Each declarator declares one identifier, and asserts that when an operand of the same form as the declarator appears in an expression, it designates a function or object with the scope, storage duration

ARC: How to release static variable?

淺唱寂寞╮ 提交于 2019-12-04 03:07:31
Will dealloc (below) release the NSString pointed to by the static variable 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 Yes, because since you did not specify an ownership qualifier, the LLVM compiler infers that exampleString has __strong ownership qualification. This means that by setting exampleString to nil

Access static variable from static method

醉酒当歌 提交于 2019-12-04 02:48:43
问题 I want to access a static variable from a static method: #!/usr/bin/env python class Messenger: name = "world" @staticmethod def get_msg(grrrr): return "hello " + grrrr.name print Messenger.get_msg(Messenger) How to do it without passing grrrr to a method? Is this the true OOP?.. Anything like name or self.name seems not working: NameError: global name 'name' is not defined and NameError: global name 'self' is not defined 回答1: Use @classmethod instead of @staticmethod . Found it just after

NullPointerException with static variables

一世执手 提交于 2019-12-03 14:15:45
I just hit very strange (to me) behaviour of java. I have following classes: 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); } And: 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();