declaration

More elegant way of declaring multiple variables at the same time

无人久伴 提交于 2019-11-26 11:46:45
问题 To declare multiple variables at the \"same time\" I would do: a, b = True, False But if I had to declare much more variables, it turns less and less elegant: a, b, c, d, e, f, g, h, i, j = True, True, True, True, True, False, True ,True , True, True Is there a better / elegant / convenient way to do this? This must be very basic, but if I do used a list or a tuple for storing the variables, how would I have to approach so that I would be helpful since: aList = [a,b] Is not valid, I would

implicit class variable declaration in php?

大城市里の小女人 提交于 2019-11-26 11:36:36
问题 I\'ve been looking at some code and am having a hard time working out variable declaration in php classes. Specifically it appears that the code i\'m looking at doesn\'t declare the class variables before it uses them. Now this may be expected but I can\'t find any info that states that it is possible. So would you expect this: class Example { public function __construct() { $this->data = array(); $this->var = \'something\'; } } to work? and does this create these variables on the class

Declaring and initializing a variable in a Conditional or Control statement in C++

谁说我不能喝 提交于 2019-11-26 11:24:41
问题 In Stroustrup\'s The C++ Programming Language: Special Edition (3rd Ed) , Stroustrup writes that the declaration and initialization of variables in the conditionals of control statements is not only allowed, but encouraged. He writes that he encourages it because it reduces the scope of the variables to only the scope that they are required for. So something like this... if ((int i = read(socket)) < 0) { // handle error } else if (i > 0) { // handle input } else { return true; } ...is good

Has the new C++11 member initialization feature at declaration made initialization lists obsolete?

家住魔仙堡 提交于 2019-11-26 10:19:33
问题 With C++11, we now have the ability to initialize class members in a header declaration: class aClass { private: int mInt{100}; public: aClass(); ~aClass(); }; So I\'m a bit confused. Traditionally initialization lists in constructors have been used for member initialization: aClass::aClass() : mInt(100) { ... } Has the new C++11 member initialization feature at declaration made initialization lists obsolete? If not, what are the advantages of one over the other? What situations would make

Can a union be initialized in the declaration?

丶灬走出姿态 提交于 2019-11-26 09:47:17
问题 For example, say we have a union typedef union { unsigned long U32; float f; }U_U32_F; When a variable of this union type is declared, is there a way to set an initial value? U_U32_F u = 0xffffffff; // Does not work...is there a correct syntax for this? 回答1: Use an initializer list: U_U32_F u = { 0xffffffff }; You can set other members than the first one via U_U32_F u = { .f = 42.0 }; 回答2: Try U_U32_F u = {0xffffffff}; 回答3: Note that per-member union initialization doesn't work on pre-C99

Redeclaring a javascript variable

爷,独闯天下 提交于 2019-11-26 09:38:50
问题 In this tutorial there is written: If you redeclare a JavaScript variable, it will not lose its value. Why should I redeclare a variable? Is it practical in some situations? thank you 回答1: It's nothing more than a reminder that if you do this: var x=5; var x; alert(x); Result will be 5. If you re-declare variable in some other languages for example - result will be undefined, or NaN, but not in javascript. 回答2: An example of redeclaring a variable can be found in Google Analytics. When the

Why is adding attributes to an already instantiated object allowed?

泄露秘密 提交于 2019-11-26 09:08:17
问题 I am studying python, and although I think I get the whole concept and notion of Python, today I stumbled upon a piece of code that I did not fully understand: Say I have a class that is supposed to define Circles but lacks a body: class Circle(): pass Since I have not defined any attributes, how can I do this: my_circle = Circle() my_circle.radius = 12 The weird part is that Python accepts the above statement. I don\'t understand why Python doesn\'t raise an undefined name error . I do

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

て烟熏妆下的殇ゞ 提交于 2019-11-26 08:53:43
问题 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

Declaring variables without var keyword

橙三吉。 提交于 2019-11-26 07:40:37
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? 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; variable1 = 5; varaible2 = 6; return variable1 + variable2; } Seems simple enough, but it returns NaN ,

How to initialize a vector in C++ [duplicate]

久未见 提交于 2019-11-26 06:54:18
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: C++: Easiest way to initialize an STL vector with hardcoded elements I want to initialize a vector like we do in case of an array. Example int vv[2] = {12, 43}; But when I do it like this, vector<int> v(2) = {34, 23}; OR vector<int> v(2); v = {0, 9}; it gives an error: expected primary-expression before ‘{’ token AND error: expected ‘,’ or ‘;’ before ‘=’ token respectively. 回答1: With the new C++ standard (may