如何使用 C++ 在一行中声明和定义多个变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6838408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I declare and define multiple variables in one line using C++?
提问by Sam
I always though that if I declare these three variables that they will all have the value 0
我总是认为,如果我声明这三个变量,它们的值都将为 0
int column, row, index = 0;
But I find that only index equals zero & the others are junk like 844553 & 2423445.
但我发现只有索引等于零,其他都是垃圾,比如 844553 和 2423445。
How can I initialise all these variables to zero without declaring each variable on a new line?
如何将所有这些变量初始化为零而不在新行上声明每个变量?
回答by Josh
int column = 0, row = 0, index = 0;
回答by Matt
When you declare:
当您声明:
int column, row, index = 0;
Only index is set to zero.
只有索引设置为零。
However you can do the following:
但是,您可以执行以下操作:
int column, row, index;
column = index = row = 0;
But personally I prefer the following which has been pointed out.
It's a more readable form in my view.
但我个人更喜欢以下已经指出的内容。
在我看来,这是一种更具可读性的形式。
int column = 0, row = 0, index = 0;
or
或者
int column = 0;
int row = 0;
int index = 0;
回答by Mateen Ulhaq
As @Josh said, the correct answer is:
正如@Josh 所说,正确答案是:
int column = 0,
row = 0,
index = 0;
You'll need to watch out for the same thing with pointers. This:
您需要注意与指针相同的事情。这个:
int* a, b, c;
Is equivalent to:
相当于:
int *a;
int b;
int c;
回答by Mark B
If you declare one variable/object per line not only does it solve this problem, but it makes the code clearer and prevents silly mistakes when declaring pointers.
如果你每行声明一个变量/对象,不仅可以解决这个问题,而且可以使代码更清晰,并防止在声明指针时犯愚蠢的错误。
To directly answer your question though, you have to initialize each variable to 0 explicitly. int a = 0, b = 0, c = 0;
.
但是,要直接回答您的问题,您必须将每个变量显式初始化为 0。int a = 0, b = 0, c = 0;
.
回答by Michael Kristofik
int column(0), row(0), index(0);
Note that this form will work with custom types too, especially when their constructors take more than one argument.
请注意,这种形式也适用于自定义类型,尤其是当它们的构造函数采用多个参数时。
回答by ivaigult
As of 2k18, you can use Structured Bindings:
从 2k18 开始,您可以使用结构化绑定:
#include <iostream>
#include <tuple>
int main ()
{
auto [hello, world] = std::make_tuple("Hello ", "world!");
std::cout << hello << world << std::endl;
return 0;
}
回答by Ajay
Possible approaches:
可能的方法:
- Initialize all local variables with zero.
- Have an array,
memset
or{0}
the array. - Make it global or static.
- Put them in
struct
, andmemset
or have a constructor that would initialize them to zero.
- 用零初始化所有局部变量。
- 有一个数组,
memset
或{0}
数组。 - 使其成为全局或静态的。
- 将它们放入
struct
, 和memset
或具有将它们初始化为零的构造函数。
回答by Levi Uzodike
I wouldn't recommend this, but if you're really into it being one line and only writing 0 once, you can also do this:
我不会推荐这个,但如果你真的喜欢它是一行并且只写 0 一次,你也可以这样做:
int row, column, index = row = column = 0;
回答by user3487742
When you declare a variable without initializing it, a random number from memory is selected and the variable is initialized to that value.
当您声明一个变量而不初始化它时,会从内存中选择一个随机数并将该变量初始化为该值。