declaration

What's the difference between these two java variable declarations?

谁说胖子不能爱 提交于 2019-12-29 09:11:54
问题 public class SomeClass { private HashSet<SomeObject> contents = new HashSet<SomeObject>(); private Set<SomeObject> contents2 = new HashSet<SomeObject>(); } What's the difference? In the end they are both a HashSet isn't it? The second one looks just wrong to me, but I have seen it frequently used, accepted and working. 回答1: Set is an interface, and HashSet is a class that implements the Set interface. Declaring the variable as type HashSet means that no other implementation of Set may be used

Can you add a condition to a variable declaration?

爷,独闯天下 提交于 2019-12-29 07:31:12
问题 This doesn't make sense to me, but I have a feeling I saw a code using this: var abc = def || ghi; My question is, is this valid? Can we add a condition to a variable declaration? I imagine the answer is no but I have this at the back of my mind that I saw something similar in code once. 回答1: This gives to abc the value of def if it isn't falsy (i.e. not false , null , undefined , 0 or an empty string), or the value of ghi if not. This is equivalent to: var abc; if (def) abc = def; else abc =

Define local function in JavaScript: use var or not?

北城余情 提交于 2019-12-29 02:48:08
问题 When a local (inner) function is declared in JavaScript, there are two options: Declaring with var keyword, assigning to the variable: (function() { var innerFunction1 = function() { ... }; innerFunction1(); }()); Declaring just with the function keyword, without assigning to variable: (function() { function innerFunction2() { ... }; innerFunction2(); }()); I can see one advantage of the second: the function can be declared below the code which calls it so it is easier to separate private

undefined C struct forward declaration

元气小坏坏 提交于 2019-12-28 05:43:08
问题 I have a header file port.h, port.c, and my main.c I get the following error: 'ports' uses undefined struct 'port_t' I thought as I have declared the struct in my .h file and having the actual structure in the .c file was ok. I need to have the forward declaration as I want to hide some data in my port.c file. In my port.h I have the following: /* port.h */ struct port_t; port.c: /* port.c */ #include "port.h" struct port_t { unsigned int port_id; char name; }; main.c: /* main.c */ #include

What is the purpose of forward declaration?

旧时模样 提交于 2019-12-28 04:26:07
问题 what is the description or meaning of this: for example: class test; class test { ..... }; 回答1: C++ (like C) was designed to be implementable by a single-pass compiler. Forward references are necessary in cases where the compiler needs to know that a symbol refers to a class before the class is actually defined. The classic example of this is when two classes need to contain pointers to each other. i.e. class B; class A { B* b; }; class B { A* a; }; Without the forward reference to B, the

Is there a use for function declarations inside functions?

℡╲_俬逩灬. 提交于 2019-12-28 03:04:42
问题 We can declare functions inside functions (I wanted a local variable, but it parses as a function declaration): struct bvalue; struct bdict { bdict(bvalue); } struct bvalue { explict operator bdict() const; } struct metainfo { metainfo(bdict); } void foo(bvalue v) { metainfo mi(bdict(v)); // parses as function declaration metainfo mi = bdict(v); // workaround // (this workaround doesn't work in the presence of explicit ctors) } Are the sole reasons "because it makes the parser simpler" and

Declaring variables without var keyword

ぃ、小莉子 提交于 2019-12-27 10:22:31
问题 At w3schools there is written: If you declare a variable, without using "var", the variable always becomes GLOBAL. Is it useful to declare global variable inside the function? I can imagine to declare some global variables in some event handler, but what is it good for? Better usage of RAM? 回答1: No, there's no RAM benefit or anything like that. What w3schools is talking about is something I call The Horror of Implicit Globals. Consider this function: function foo() { var variable1, variable2;

Declaring variables without var keyword

不羁的心 提交于 2019-12-27 10:21:23
问题 At w3schools there is written: If you declare a variable, without using "var", the variable always becomes GLOBAL. Is it useful to declare global variable inside the function? I can imagine to declare some global variables in some event handler, but what is it good for? Better usage of RAM? 回答1: No, there's no RAM benefit or anything like that. What w3schools is talking about is something I call The Horror of Implicit Globals. Consider this function: function foo() { var variable1, variable2;

How does the Java array argument declaration syntax “…” work?

风流意气都作罢 提交于 2019-12-25 18:49:31
问题 I have been writing java for a while, and today I encountered the following declaration: public static void main(String... args) { } Note the "dot dot dot" in the array declaration, rather than the usual bracket []. Clearly it works. In fact I wrote a small test and verified it works. So, I pulled the java grammar to see where this syntax of argument declaration is, but did not find anything. So to the experts out there, how does this work? Is it part of the grammar? Also, while I can declare

C imlicit declaration of a function

↘锁芯ラ 提交于 2019-12-25 16:08:30
问题 I am on Linux and gcc 4.2.3. For the below code portion, lp_parm_talloc_string function is called implicitly and afterwards it is defined: char *lp_parm_string(const char *servicename, const char *type, const char *option) { return lp_parm_talloc_string(lp_servicenumber(servicename), type, option, NULL); } /* Return parametric option from a given service. Type is a part of option before ':' */ /* Parametric option has following syntax: 'Type: option = value' */ /* the returned value is