variable-declaration

C++, variable declaration in 'if' expression

北城余情 提交于 2019-11-26 04:42:58
问题 What\'s going on here? if(int a = Func1()) { // Works. } if((int a = Func1())) { // Fails to compile. } if((int a = Func1()) && (int b = Func2())) ) { // Do stuff with a and b. // This is what I\'d really like to be able to do. } Section 6.4.3 in the 2003 standard expains how variables declared in a selection statement condition have scope that extends to the end of the substatements controlled by the condition. But I don\'t see where it says anything about not being able to put parenthesis

Is there any overhead to declaring a variable within a loop? (C++) [duplicate]

大兔子大兔子 提交于 2019-11-26 02:51:26
This question already has an answer here: Difference between declaring variables before or in loop? 25 answers I am just wondering if there would be any loss of speed or efficiency if you did something like this: int i = 0; while(i < 100) { int var = 4; i++; } which declares int var one hundred times. It seems to me like there would be, but I'm not sure. would it be more practical/faster to do this instead: int i = 0; int var; while(i < 100) { var = 4; i++; } or are they the same, speedwise and efficiency-wise? Stack space for local variables is usually allocated in function scope. So no stack

Declaring variables inside loops, good practice or bad practice?

扶醉桌前 提交于 2019-11-26 01:55:50
问题 Question #1: Is declaring a variable inside a loop a good practice or bad practice? I\'ve read the other threads about whether or not there is a performance issue (most said no), and that you should always declare variables as close to where they are going to be used. What I\'m wondering is whether or not this should be avoided or if it\'s actually preferred. Example: for(int counter = 0; counter <= 10; counter++) { string someString = \"testing\"; cout << someString; } Question #2: Do most

Is there any overhead to declaring a variable within a loop? (C++) [duplicate]

孤者浪人 提交于 2019-11-26 01:12:33
问题 This question already has answers here : Difference between declaring variables before or in loop? (25 answers) Closed 2 years ago . I am just wondering if there would be any loss of speed or efficiency if you did something like this: int i = 0; while(i < 100) { int var = 4; i++; } which declares int var one hundred times. It seems to me like there would be, but I\'m not sure. would it be more practical/faster to do this instead: int i = 0; int var; while(i < 100) { var = 4; i++; } or are

Python Variable Declaration

家住魔仙堡 提交于 2019-11-26 00:57:48
问题 Learning Python, and has some basic doubts. 1.I have seen variable declaration (path here) as class writer: path = \"\" sometimes, no explicit declaration but initialize through __init__ . def __init__(self, name): self.name = name I understand the purpose of __init__ , but is it advisable to declare variable in any other functions. 2.How can I create variable to hold a custom type? class writer: path = \"\" # string value customObj = ?? 回答1: Okay, first things first. There is no such thing

What does “var FOO = FOO || {}” (assign a variable or an empty object to that variable) mean in Javascript?

不想你离开。 提交于 2019-11-25 23:59:39
问题 Looking at an online source code I came across this at the top of several source files. var FOO = FOO || {}; FOO.Bar = …; But I have no idea what || {} does. I know {} is equal to new Object() and I think the || is for something like \"if it already exists use its value else use the new object. Why would I see this at the top of a source file? 回答1: Your guess as to the intent of || {} is pretty close. This particular pattern when seen at the top of files is used to create a namespace , i.e. a

When to use extern in C++

拥有回忆 提交于 2019-11-25 22:43:03
问题 I\'m reading \"Think in C++\" and it just introduced the extern declaration. For example: extern int x; extern float y; I think I understand the meaning (declaration without definition), but I wonder when it proves useful. Can someone provide an example? 回答1: This comes in useful when you have global variables. You declare the existence of global variables in a header, so that each source file that includes the header knows about it, but you only need to “define” it once in one of your source

C pointer to array/array of pointers disambiguation

▼魔方 西西 提交于 2019-11-25 21:42:17
问题 What is the difference between the following declarations: int* arr1[8]; int (*arr2)[8]; int *(arr3[8]); What is the general rule for understanding more complex declarations? 回答1: int* arr[8]; // An array of int pointers. int (*arr)[8]; // A pointer to an array of integers The third one is same as the first. The general rule is operator precedence. It can get even much more complex as function pointers come into the picture. 回答2: Use the cdecl program, as suggested by K&R. $ cdecl Type `help'