Possible Duplicate:
Is there a difference in C++ between copy initialization and direct initialization?
Is there any difference between,
int x(5);
int x = 5;
& What's the difference between,
int const x(5);
const int x(5);
Possible Duplicate:
Is there a difference in C++ between copy initialization and direct initialization?
Is there any difference between,
int x(5);
int x = 5;
& What's the difference between,
int const x(5);
const int x(5);
Of the 4 lines you show, only 1 is valid C.
int x(5); /* INVALID C */
int x = 5;
int const x(5); /* INVALID C */
const int x(5); /* INVALID C */
I don't know the meaning of the invalid lines in other languages. In C, the meaning of the valid line is to create an object with name x
, type int
, and value 5
.
To define a "const" (better: a "read-only") object, the C syntax is
const int x = 5;
int const x = 5;
but x
cannot be used in places where only constants are allowed. x
is not a constant: it is an object of type const int
#define CONSTANT 42
enum /*unnamed*/ { First=1, Second, Third };
switch (var) {
case x: break; /* invalid use of x; only constants allowed in case labels */
case 15: break; /* ok; 15 is a constant */
case CONSTANT: break; /* ok; CONSTANT is a constant with value 42 */
case Second: break; /* ok; Second is a constant with value 2 */
}
For the first, there's no difference for built-in types. For class types, the T x = y
notation requires a copy constructor, the T x(y)
notation doesn't. EDIT: Also, as one of the commenters pointed out, T x = y
will not work if the T(y)
constructor is declared explicit
.
For the second, there's no difference as far as the compiler is concerned, but the second (const int
) can lead to confusion in many cases, especially when typedef
s are involved, e.g.:
typedef int* IntPtr;
IntPtr const cpi1; // Const pointer to a non-const int.
const IntPtr cpi2; // Also a const pointer to a non-const int.
In both definitions, the const
applies to the pointer.
Because of this confusion, it's generally considered better practice to put the const
after what it modifies. Now—for a long time, putting the const
at the start was usual, with the result that the practice is widespread.
A really good rule of thumb regarding const
:
Read Declarations Right-to-Left.
(see Vandevoorde/Josutiss "C++ Templates: The Complete Guide")
E.g.:
int const x; // x is a constant int
const int x; // x is an int which is const
// easy. the rule becomes really useful in the following:
int const * const p; // p is const-pointer to const-int
int const &p; // p is a reference to const-int
int * const * p; // p is a pointer to const-pointer to int.
Ever since I follow this rule-of-thumb, I never misinterpreted such declarations again.
(: sisab retcarahc-rep a no ton ,sisab nekot-rep a no tfel-ot-thgir naem I hguohT :tidE
No both are exactly same until you are using an Argument. It's just a matter of coding style.
[Note: But Following are not same:
int x; // x is variable
int x(); // x is function declaration
]
About the const
keyword:
So, int const i
and const int i
are the same.
But, int *const i
and const int *i
are not the same: in the first expression, the pointer is constant, in the second expression, the integer pointed to is constant. Als int const* i
is equivalent to the second one.
No difference. Alternate ways to initialize the variable to a value.
But did you try this on your system? What did you get?
int x(5);
int x = 5;
No difference.
int const x(5);
const int x(5);
No difference.
int x = 5;
const int *p = &x;
int *const q = &x;
Here is a difference in p
and q
. Using p
you can't change the value of x
, but you can assign a new address to p
. Using q
, you cannot point to any other memory location, but you can modify x
using q
.