in C++ you declare and define variables.When you use:
int a;
you are just declaring a as an integer, and you have to define it later,
but as jsmith said, it is perfectly valid to declare and define a variable in a single
statement, like:
int a = 4;
and in case you are wondering, you can also do this:
int a = 4, b = 7;
or if they are supposed to be initialized to the same value, even:
Or you can make it look like object construction which is how I like to do it. This makes it clearer that the variable a is being constructed and initialized with a value of 4.
1 2 3 4 5
int a(4);
int a = 4; // equivalent to previous line but looks more like assignment
int a; // object constructed but not initialized
a = 4; // value of 4 is assigned to the object.
The closest you can get to such a construct is the fugly int a = 7, b = a;. Personally, I think it is just better to declare each variable on its own line:
It's illegal syntax. The only time you can do something close to that is if all variables are declared prior. For example, in a default constructor call you could do this:
Thanks for the answers. I think you guys misunderstood my question, but that may be because i asked it in a wrong way. but wmheric got me the answer thanks.