static-variables

Static variable order [duplicate]

僤鯓⒐⒋嵵緔 提交于 2021-02-20 13:32:23
问题 This question already has answers here : Static field initialization order (C#) - can someone explain this snippet? (6 answers) Closed 7 years ago . I have aproblem with the order of static variable declaration in C# When i run this code: static class Program { private static int v1 = 15; private static int v2 = v1; static void Main(string[] args) { Console.WriteLine("v2 = "+v2); } } The output is: v2=15 But when i change the static variable declaration order like this: static class Program {

No linker error when global variable declared static in the header file [duplicate]

六眼飞鱼酱① 提交于 2021-02-19 03:46:48
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Static variables in C++ // x.h int i = 3; // x1.cpp #include"x.h" //... // x2.cpp #include"x.h" //... Above code will give linker error. However If I declare, //x.h static int i = 3; It doesn't give linker error in gcc, even we have the same #include ! Are we creating different static int i; for every .cpp file ? Will it cause any silent linking bug (due to same name)? 回答1: Are we creating different static int i

linking error : multiple definition of static variable

邮差的信 提交于 2021-01-01 04:58:31
问题 So I wrote the following code first and was getting a compile error. After reading this answer : static array class variable "multiple definition" C++ I modified my code and moved the static variable definition to a cpp file and it executes fine, but I'm unable to understand that when I have used pre-processor guards, why is it showing multiple definition error ? #ifndef GRAPH_H #define GRAPH_H #include<iostream> #include<vector> using namespace std; struct node{ int element=0; static vector

Why it is allowed to initialize static variable with non const here?

纵饮孤独 提交于 2020-06-08 05:57:29
问题 I was reading this. The first answer by @Andrei T says that A "large" object is never a constant expression in C, even if the object is declared as const. Const-qualified objects (of any type) are not constants in C language terminology. They cannot be used in initializers of objects with static storage duration, regardless of their type. For example, this is NOT a constant const int N = 5; /* `N` is not a constant in C */ The above N would be a constant in C++, but it is not a constant in C.

Why it is allowed to initialize static variable with non const here?

倾然丶 夕夏残阳落幕 提交于 2020-06-08 05:57:11
问题 I was reading this. The first answer by @Andrei T says that A "large" object is never a constant expression in C, even if the object is declared as const. Const-qualified objects (of any type) are not constants in C language terminology. They cannot be used in initializers of objects with static storage duration, regardless of their type. For example, this is NOT a constant const int N = 5; /* `N` is not a constant in C */ The above N would be a constant in C++, but it is not a constant in C.

'… incorrectly extends base class static side' error when overriding static field in derived class

大城市里の小女人 提交于 2020-01-23 07:01:28
问题 Overriding static field in derived class causes error TS2417: Build:Class static side 'typeof TDerived' incorrectly extends base class static side 'typeof TBase'. Is this a legit error case? class TBase { private static s_field = 'something'; public constructor() {} } class TDerived extends TBase { private static s_field = 'something else'; // commenting this line fixes error public constructor() { super(); } } How should i deal with static fields then? The only workaround right now would be

C++ static variables initialization order

守給你的承諾、 提交于 2020-01-13 02:13:12
问题 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

Possible to test if a variable is static in PHP?

假装没事ソ 提交于 2020-01-12 23:04:32
问题 Is it possible to test if a variable is static in PHP? I am trying create a magic method __get that also looks at static variables. I find that property_exists() returns true when a variable is static too. But I will need to use :: instead of -> I'd expect? 回答1: It is possible to test if a variable is static via Reflection: class Foo { static $bar; } $prop = new ReflectionProperty('Foo', 'bar'); var_dump($prop->isStatic()); // TRUE However, that still won't allow you to use them with magic

NullPointerException with static variables

北城余情 提交于 2020-01-12 08:05:51
问题 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

NullPointerException with static variables

孤街醉人 提交于 2020-01-12 08:05:44
问题 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