Reputation: 15062
What is the difference between the statements of a = 5
, a(5)
, a{5}
and a[5]
in C++?
I occasionally see them used for anything like assigning a value to an object in C++ programs. Where is the difference between them?
Upvotes: 1
Views: 819
Reputation: 15062
There is a main difference between all of these statements in C++ in the context of initialization ( which is done at the moment of the declaration of an object) and assignment; the last statement a[5]
is also a completely different thing in comparison to the others.
Premise: If a
is an object of a certain datatype, for example int
, we can assign an appropriate value (according to the datatype declared with) to a
. Note: The value may be evaluated by a valid expression, for example a = 2 + 3;
behave the same as a = 5;
.
a = 5;
This is the common assignment form used in C++ programs. It can be used either for the initialization or later assignment (after the declaration of a
) in the respective scope. If a
is declared properly as of type int
, this statement assigns the integer value of 5
to the int
variable of a
.
It can be used either directly by the declaration:
int a = 5;
first assignment (when a
is not initialized at its declaration):
int a;
// some code may exist between.
a = 5;
Or an assignment anywhere else in the respective scope after the first assignment or initialization at the declaration (dependent on storage class):
int a; // declaration of `a`.
// some code may exist between.
a = 5; // first assignment of `a` with the value of `5`.
// some code may exist between.
a = 7; // assignment of the value of `7` to `a`.
a(5);
This statement initializes the int
variable of a
with the value of 5
. It can be used only at the explicit initialization at the moment of the declaration:
int a(5);
Else the compiler will high-probably throw an error, because it "thinks" that a
would be a function.
Technically, a(5)
could be a function, which takes the value of 5
as argument in general tough, but it is the context of the declaration of an int
object what makes the difference here.
a{5};
Same as 2. It initializes a
with the value of 5
. It can be used only by the initialization at the moment of the declaration:
int a{5};
Else the compiler will throw an error.
Note: To initialize a value like int a{5};
or int a(5)
is a C++ feature only. It is not possible to initialize an object in C this way.
a[5];
With this statement we do not apparently initialize or assigning the value of 5
to the variable of a
. Instead, we defining an array, a cluster of objects, consisted of 5
objects, which is represented as its own entity by the identifier of a
:
int a[5]; // declaration of `a` - `a` is an array of 5 int objects.
So, we do not have a single object here nor assigning any value to it.
Furthermore, we have to distinguish between the declaration of an array with:
int a[5];
and the later accessing to a certain int
object of the array a
, like fore example:
a[4] = 12;
In this case, a[4]
is representing the 5
th object inside the array and the value of 12
is assigned to that fifth object of the array.
The number inside the square brackets, here 4
, is called the index number. Note, that the index numbers successively start at 0
. Thus, a[0]
represents the first object in the array, a[1]
the second, and so on. You can´t use a[5]
in that way, if you declared a
with int a[5];
because it would represent, in this case, a sixth object, which isn´t allocated in memory for a
.
Upvotes: 5