C ++ declares multiple variables in one line

I know that the declaration of variables, such as this int a = 10, b = 15, c = 20, perhaps, and that's fine, but is it possible in any program in the programming language C ++, declare variables like this int a, b, c = 10, 15, 20, where ashould be 10, bshould be 15and cequal 20.

Is this possible, and is this the right way to declare variables like this in C ++?

EDIT : Is this possible with an overload operator =?

+4
source share
2 answers

The compiler will give an error for such declarations

int a, b, c = 10, 15, 20; 

The only idea that comes to my mind is the following :)

int a, b, c = ( a = 10, b = 15, 20 ); 

Or you can make these names members of the structure data.

struct { int a, b, c; } s = { 10, 20, 30 };

EDIT: =?

. . .:)

+7
int a, b, c = 10, 15, 20;

( , , c 20 ( ) a b .

c-array/std:: array/ std::vector :

int carray[3] = {10, 15, 20};
std::array<int, 3> a = {10, 15, 20};
std::vector<int> v = {10, 15, 20};

carray[0] == a[0] && a[0] == v[0] && v[0] == 10

+1

All Articles