global-variables

C++ global extern constant defined at runtime available across multiple source files

醉酒当歌 提交于 2019-12-23 13:04:05
问题 I have an integer constant that is to be defined at runtime. This constant needs to be available globally and across multiple source files. I currently have the following simplified situation: ClassA.h declares extern const int someConstant; ClassA.cpp uses someConstant at some point. Constants.h declares extern const int someConstant; main.cpp includes ClassA.h and Constants.h , declares const int someConstant , and at some point during main() tries to initialize someConstant to the real

What is the best way to store a value for use in a later function? I'm hearing global variables are evil

烈酒焚心 提交于 2019-12-23 12:29:08
问题 So the code I'm using is at http://jsfiddle.net/8j947/10/ and it returns a value of true or false for the variable isLive. How can I use the variable onLive in a later function? I read the answers at Accessing variables from other functions without using global variables, but I'm having a lot of trouble getting it to work. All I want is to store the value, true or false, so I can call it into an if statement in a later function. Can anyone give me the simplest way to do this? Is there a way

Global Variable, subroutine variable Question in Perl

久未见 提交于 2019-12-23 12:27:22
问题 How can I transfer the subroutine variable value into another subroutine variable, Can I use global variable. sub foo(){ my $myvar = "Hello"; } sub foo1(){ my $myvar1 = $myvar; # how can I get the "Hello" from $myvar. } I tried to use package and global variable, but failed. Package Bar; our $bar; Thank you. 回答1: You could declare the variable in a scope that includes the 2 functions: { my $myvar sub foo{ $myvar = "Hello"; } sub foo1{ my $myvar1 = $myvar; } } That is not really elegant though

Make A javascript variable only avaliable to functions on the same .js file

喜夏-厌秋 提交于 2019-12-23 12:25:11
问题 I somewhat new to Javascript and I'm stuck on this one item. Can someone please show me how to make a javascript variable only usable on the .js file that it is on. EXAMPLE: Say I have two .js files, PG1 & PG2. PG1 contains var channel = Channel01; PG2 contains a variable with the same name, but a different entered Variable (the part after the equals sign) (var channel = Channel02) I don't want function1 on PG1 to use the variable on PG2 or for function2 on PG2 to use the variable on PG1. I

Python - Delete (remove from memory) a variable from inside a function?

人盡茶涼 提交于 2019-12-23 12:14:47
问题 I have to load this massive object A (that can weight almsot 10go) that I need to pass to a function, that extracts from it a parameter B to further exert some heavy computations on it. A = load(file) def function(A): B = transorm(A) B = compute(B) return(B) In order to free some memory (as I already had a MemoryError), I'd like to remove A from memory right after its transformation to B. I tried del but it doesn't seem to affect the existence of A at the script level. I also tried del global

Is the “static initialization order fiasco” a concern for constexpr variables?

老子叫甜甜 提交于 2019-12-23 10:54:38
问题 If I initialize a constexpr variable foo with a non-default value in one translation unit and then a initialize another constexpr variable bar with foo in another translation unit is it possible for bar to be initialized before foo resulting in a bar that was initialized by a zero-or-default-initialized foo . i.e. Unlike in the non-constexpr case (where the static initialization order fiasco is in effect) will the compiler and linker analyze dependency ordering to guarantee the correct result

Use a variable from __construct() in other methods

穿精又带淫゛_ 提交于 2019-12-23 09:31:43
问题 I defined a new variable in __construct() and I want to use it in another function of this class . But my variable is empty in the other function! this is my code: class testObject{ function __construct() { global $c; $data = array("name"=>$c['name'], "family"=>$c['family']); } function showInfo() { global $data; print_r($data); } } 回答1: Declare variable $data as global inside the constructor: function __construct() { global $c; global $data; $data = array("name"=>$c['name'], "family"=>$c[

Returning a variable while using a post increment in C

巧了我就是萌 提交于 2019-12-23 08:56:08
问题 I have a global variable called var and a function foo . (I know it's a bad practice but sometimes it's unavoidable) I'm wondering if the C standard (I'm compiling using c99) says what happens to var if I try to execute: long foo(){ return var++; } Thanks. 回答1: Short answer: It will return a copy of var and then immediately afterwards increment the global var . Long answer: C11 6.5.2.4 "The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the

Python: Regarding variable scope. Why don't I need to pass x to Y?

巧了我就是萌 提交于 2019-12-23 08:52:56
问题 Consider the following code, why don't I need to pass x to Y? class X: def __init__(self): self.a = 1 self.b = 2 self.c = 3 class Y: def A(self): print(x.a,x.b,x.c) x = X() y = Y() y.A() Thank you to the top answers, they really helped me see what was the problem, namely misunderstanding regarding variable scope. I wish I could choose both as correct answer as they are enlightening in their own way. 回答1: From The Python Tutorial: Although scopes are determined statically, they are used

Scope of variables (Hoisting) in Javascript

流过昼夜 提交于 2019-12-23 08:52:50
问题 One of my friends was taking an online quiz and he asked me this question which I could not answer. var global = false; function test() { global = true; return false; function global() {} } console.log(global); // says false (As expected) test(); console.log(global); // says false (Unexpected: should be true) If we assume that functions are hoisted at the top along with var variables, let's try this one. var foo = 1; function bar() { return foo; foo = 10; function foo() {} var foo = 11; } bar