variable-declaration

window[name] equivalent to dynamically access const and let declarations

て烟熏妆下的殇ゞ 提交于 2019-12-06 16:00:59
The old style JavaScript var declaration outside of a closure is global (top-level scope) and can be accessed in a browser from the window object. For example, the declaration var x = 3; can be accessed with window['x'] . How do you similarly access a const or let declaration given the name (string) of the declaration? var x = 3; const y = 7; let z = 21; console.log('x = ' + window['x']); //x = 3 console.log('y = ' + window['y']); //y = undefined console.log('z = ' + window['z']); //z = undefined For the above example, how do you get the values 7 and 21 for "y" and "z" instead of undefined ?

Is long long a type in C?

僤鯓⒐⒋嵵緔 提交于 2019-12-05 10:29:16
问题 I know the title seems quite stupid, but I think it's worth asking. Take this declaration(or definition, maybe) for example: _Thread_local long volatile static int _Atomic const long unsigned x = 10; I used to consider long long as a type, but if it's a type name, how can so many qualifiers be inserted into it? So I consulted N1570 with this question, only to be more confused. It mentions some terms such as " type-specifier " and " type-qualifier ", and I can't find long long in "type

Declaring Variables in @implementation

走远了吗. 提交于 2019-12-05 05:48:29
I saw an example in a book showing this code: @implementation ViewController { NSString *name; } Why not declare this in @interface ? What's the difference in declaring variables in @implementation instead of @interface ? Why declare this NSString in a scope? The advantage of declaring ivars in the @implementation section is better encapsulation. That way, ivars don't have to appear in the .h file and are therefore not visible to external users of your class who only get to see the header file. This better hides the internal implementation of the class. Generally speaking, now that properties

what's wrong with declaring a variable inside if's condition?

淺唱寂寞╮ 提交于 2019-12-04 22:49:52
Perhaps I am getting rusty (have been writing in Python recently). Why does this not compile? if ( (int i=f()) == 0) without the () around the int i=f() I get another, much more reasonable error of i is not being boolean. But that's why I wanted the parentheses in the first place! My guess would be that using the parentheses makes it into an expression, and that declaration statements are not allowed in an expression. Is it so? And if yes, is it one of the C++'s syntax quirks? BTW, I was actually trying to do this: if ( (Mymap::iterator it = m.find(name)) != m.end()) return it->second; You can

Skip variable declaration using goto?

末鹿安然 提交于 2019-12-04 05:53:05
I am reading C Programming - A Modern Approach by K.N.King to learn the C programming language and it was noted that goto statements must not skip variable-length array declarations. But now the question is: Why are goto jumps allowed to skip fixed-length array declarations and ordinary declarations? And more precisely, what is the behavior of examples like these, according to the C99 standard? When I tested these cases it seemed like the declarations were actually not jumped over, but is that correct? Are the variables whose declarations might have been jumped over safe to use? 1. goto later;

Re-declaring object in a for loop - C++

隐身守侯 提交于 2019-12-04 05:00:16
问题 I do have a question on variable re-declaration in loops. Why declaring an object in a foor loop doesn't trigger the redeclaration error? Do the object get destroyed and recreated at each iteration of the loop? I'm inserting a sample code class DataBlock { int id; string data; public: DataBlock(int tid=0,const string &tdata=""){ id=tid; data=tdata; } } int main(int argc, char *argv[]){ ifstream file; int temp_id; //temporary hold the the id read from the file string temp_data; //temporary

Is long long a type in C?

巧了我就是萌 提交于 2019-12-04 00:21:11
I know the title seems quite stupid, but I think it's worth asking. Take this declaration(or definition, maybe) for example: _Thread_local long volatile static int _Atomic const long unsigned x = 10; I used to consider long long as a type, but if it's a type name, how can so many qualifiers be inserted into it? So I consulted N1570 with this question, only to be more confused. It mentions some terms such as " type-specifier " and " type-qualifier ", and I can't find long long in "type specifiers", but isn't long long a primitive type in C? There are so many books telling me so! Clarifying not

Why doesn't C# let you declare multiple variables using var?

有些话、适合烂在心里 提交于 2019-12-03 15:31:15
问题 Given the following: // not a problem int i = 2, j = 3; so it surprises me that this: // compiler error: Implicitly-typed local variables cannot have multiple declarators var i = 2, j = 3; doesn't compile. Maybe there is something I don't understand about this (which is why I'm asking this)? But why wouldn't the compiler realize that I meant: var i = 2; var j = 3; which WOULD compile. 回答1: It's just another point of possible confusion for the programmer and the compiler. For example this is

Why doesn't C# let you declare multiple variables using var?

走远了吗. 提交于 2019-12-03 05:13:54
Given the following: // not a problem int i = 2, j = 3; so it surprises me that this: // compiler error: Implicitly-typed local variables cannot have multiple declarators var i = 2, j = 3; doesn't compile. Maybe there is something I don't understand about this (which is why I'm asking this)? But why wouldn't the compiler realize that I meant: var i = 2; var j = 3; which WOULD compile. James Gaunt It's just another point of possible confusion for the programmer and the compiler. For example this is fine: double i = 2, j = 3.4; but what does this mean? var i = 2, j = 3.4; With syntactic sugar

How to create a string-type variable in C

扶醉桌前 提交于 2019-12-03 00:24:04
Question How to declare a string variable in C? Background In my quest to learn the basics of c , I am trying to port one of my oldest python programs, Bob , to C. In the program, the script asks the user for information on him or herself, and then spits out responses. Almost all of these variables use raw_input for their information - the variables are strings. But, I have found no way to declare C variables. Code So far, I have tried to declare the variable as of type char and int. Here is the code, switch the type at your leisure. int main(int argc, const char * argv[]) { int name; printf(