| vijirin (13) | |
|
Hi all, While using a copy constructor we are passing a const reference as parameter for the constructor. Why can't we use a pointer instead of reference?. Does this sounds silly???? Regards | |
|
|
|
| Zaita (2400) | |
| Edit: See Below | |
|
Last edited on
|
|
| exception (323) | ||
|
A pointer *can* guarantee the const-ness of the data pointed to: int* const x; is quite different from const int* x; While the former statement states that the value of the pointer cannot change (i.e., you cannot write x = &y), the latter states that the data pointed to cannot change (i.e., you cannot write *x = 5). The standard defines copy-constructors as (12.8-2):
So, the answer to your question is: because it is defined this way in the standard. | ||
|
|
||
| grecoman (7) | |
|
the reason is because when you use a reference it´s become more easy to modify | |
|
|
|