variable-declaration

Type Inference: Java 7 Type Parameters

↘锁芯ラ 提交于 2019-12-23 04:45:56
问题 today we talked about advantages of Java 7 in our company. Since Java 7 there is the possibility to define following line Map<String, List<String>> myMap = new HashMap<String, List<String>>(); as Map<String, List<String>> myMap = new HashMap<>(); We had a long discussion about the topic described above. Some of us had the opinion that this is type inference (like var keyword in C#) and the type will be computed at runtime, others thought it's only a simpler way to declare some variable and

Declaring Variables in @implementation

混江龙づ霸主 提交于 2019-12-22 04:49:09
问题 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? 回答1: 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

Skip variable declaration using goto?

与世无争的帅哥 提交于 2019-12-21 13:06:18
问题 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

C++11 auto declaration with and without pointer declarator

微笑、不失礼 提交于 2019-12-20 10:16:35
问题 What's the difference between the types of bar1 and bar2 ? int foo = 10; auto bar1 = &foo; auto *bar2 = &foo; If both bar1 and bar2 are int* , does it makes sense to write the pointer declarator ( * ) in the bar2 declaration? 回答1: The declarations are exactly equivalent. auto works (almost) the same as template type deduction. Putting the star explicitly makes the code a bit easier to read, and makes the programmer aware that bar2 is a pointer. 回答2: Using auto * "documents intention". And

“missing value where TRUE/FALSE needed” Error in if statement in R [duplicate]

萝らか妹 提交于 2019-12-20 07:09:34
问题 This question already has answers here : Error in if/while (condition) {: missing Value where TRUE/FALSE needed (2 answers) Closed 4 years ago . I am trying to count and print the cases in which the values in second and third columns of my dataframe named 'DATA'. But I have "missing value where TRUE/FALSE needed" Error. Could you help me please? How can I write my condition in if statement without getting this error? My Code: deneme<-function(id=vector()){ i<-1 counter<-1 sulfate<-DATA[,2]

How is “void()” useful?

落爺英雄遲暮 提交于 2019-12-20 04:24:28
问题 You cannot declare a void variable: void fn() { void a; // ill-formed } Yet this compiles: void fn() { void(); // a void object? } What does void() mean? How is it useful? Why is void a; ill-formed, while void() OK? void fn() { void a = void(); // ill-formed } 回答1: The statement void(); creates a void value and then discards it. (You can't actually do much with a void value other than discard it or return it.) The standard† says in 5.2.3 [expr.type.conv The expression T(), where T is a simple

Does C11 allow variable declarations at any place in a function?

不打扰是莪最后的温柔 提交于 2019-12-20 02:15:22
问题 Does the C11 standard (note I don't mean C++11) allow you to declare variables at any place in a function? The code below is not valid in ANSI C (C89, C90): int main() { printf("Hello world!"); int a = 5; /* Error: all variables should be declared at the beginning of the function. */ return 0; } Is it valid source code in C11? 回答1: Yes. This was already valid in C99 (see the second bullet here). 回答2: More or less. C99 introduced the ability to declare variables part way through a block and in

Create a variable to hold objects of different types C++

。_饼干妹妹 提交于 2019-12-20 01:49:04
问题 I have 3 different objects A , B and C . Depending on the the parameter given, I would like to choose among these different objects. In programming, class A { public: void printHello() { cout << "HELLO A" << endl; } }; class B { public: void printHello() { cout << "HELLO B" << endl; } }; class C { public: void printHello() { cout << "HELLO C" << endl; } }; int main () { string key = "c"; A a; B b; C c; Object obj; // I don't know how to declare Object. if (key == "a") obj = a; else if (key ==

javascript var statement and performance

大憨熊 提交于 2019-12-19 17:45:40
问题 Option1 : multiple var without assignment function MyFunction() { var a = null; var b = null; .... var z = null; a = SomeValue; b = SomeValue2; .... } Option 2: one var statement, no assignment function MyFunction() { var a, b ..., z; a = SomeValue; b = SomeValue2; .... } Option 3: multiple var statements with assignment function MyFunction() { var a = SomeValue; var b = SomeValue2; .... var z = SomeValue26; } Is there any performance benefit of using a particular option? Is it true for both

Is there any difference between `List x;` and `List x()`

我只是一个虾纸丫 提交于 2019-12-18 06:48:33
问题 The title comes from the famous site C++ FAQ by Marshall Cline. The author claims that there is a difference between the following two code examples. Suppose that List is the name of some class. Then function f() declares a local List object called x: void f() { List x; // Local object named x (of class List) ... } But function g() declares a function called x() that returns a List: void g() { List x(); // Function named x (that returns a List) ... } But is it really wrong to use the second