Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ declaring multiple variables in the same line

I know that declaring variables like this int a = 10, b = 15, c = 20 is possible and it's ok, but is it possible in any program in c++ programming language, to declare variables like this int a, b, c = 10, 15, 20 where a need to be 10, b need to be 15 and c to be 20.

Is this possible and is it right way to declare variables like this in c++?

EDIT: Is it possible with the overloading operator =?

like image 979
Almir Avatar asked Jan 08 '23 03:01

Almir


1 Answers

The compiler will issue an error for such declarations

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

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

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

Or you could make these names data members of a structure

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

EDIT: Is it possible with the overloading operator =?

There is not used the copy asssignment operator. It is a declaration. The copy assignment operator is used with objects that are already defined.:)

like image 135
Vlad from Moscow Avatar answered Jan 10 '23 17:01

Vlad from Moscow