F
fl
Hi,
I am learning C++ with "C++ primer" Fourth edition by Stanley B.
Lippman, Josee Lajoie and Barbara E. Moo.
I have a difficuty when I learn Chapter 7. Funtions. More
specifically, 7.2.1. Nonreference Parameters.
In passage on "const Parameters", it first gives an example:
const int i = 3, j = 6;
int k = gcd(3, 6); // ok: k initialized to 3
It says that it works no matter int or const int for gcd's argument.
My problem is the following. I copy the detail contents here. I type
these words as possible as the original text except I cannot type
italic font here. I don't understand the meaning of these paragraphs.
What is the implication of the two void fcn function? Does these
paragraphs tell me that
void fcn(const int i) { /* fcn can read butnot write to i */ }
is not supported by C++?
Could you explain it to me? Thanks.
The most puzzled me is the except from below:
---------------------------------
What may be surprising, is that although the parameter is a const
inside the function, the compiler otherwise treats the definition of
fcn as if we had defined the parameter as a plain int:
void fcn(const int i) { /* fcn can read but not write to i */ }
void fcn(int i) { /* .........*/ } // error:
redefines fcn(int)
---------------------------------
.............
If we make the parameter a const nonreference type:
void fcn(const int i) { /* fcn can read butnot write to i */ }
then the function cannot change its local copy of the argument. The
argument is still passed as a copy so we can pass fcn either a const
or nonconst object.
What may be surprising, is that although the parameter is a const
inside the function, the compiler otherwise treats the definition of
fcn as if we had defined the parameter as a plain int:
void fcn(const int i) { /* fcn can read but not write to i */ }
void fcn(int i) { /* .........*/ } // error:
redefines fcn(int)
This usage exists to support compatibility with the C language, which
makes no distinction between functions taking const or nonconst
parameters.
.......................................................
I am learning C++ with "C++ primer" Fourth edition by Stanley B.
Lippman, Josee Lajoie and Barbara E. Moo.
I have a difficuty when I learn Chapter 7. Funtions. More
specifically, 7.2.1. Nonreference Parameters.
In passage on "const Parameters", it first gives an example:
const int i = 3, j = 6;
int k = gcd(3, 6); // ok: k initialized to 3
It says that it works no matter int or const int for gcd's argument.
My problem is the following. I copy the detail contents here. I type
these words as possible as the original text except I cannot type
italic font here. I don't understand the meaning of these paragraphs.
What is the implication of the two void fcn function? Does these
paragraphs tell me that
void fcn(const int i) { /* fcn can read butnot write to i */ }
is not supported by C++?
Could you explain it to me? Thanks.
The most puzzled me is the except from below:
---------------------------------
What may be surprising, is that although the parameter is a const
inside the function, the compiler otherwise treats the definition of
fcn as if we had defined the parameter as a plain int:
void fcn(const int i) { /* fcn can read but not write to i */ }
void fcn(int i) { /* .........*/ } // error:
redefines fcn(int)
---------------------------------
.............
If we make the parameter a const nonreference type:
void fcn(const int i) { /* fcn can read butnot write to i */ }
then the function cannot change its local copy of the argument. The
argument is still passed as a copy so we can pass fcn either a const
or nonconst object.
What may be surprising, is that although the parameter is a const
inside the function, the compiler otherwise treats the definition of
fcn as if we had defined the parameter as a plain int:
void fcn(const int i) { /* fcn can read but not write to i */ }
void fcn(int i) { /* .........*/ } // error:
redefines fcn(int)
This usage exists to support compatibility with the C language, which
makes no distinction between functions taking const or nonconst
parameters.
.......................................................