Reference question
-
It is said that "Once a reference is initialized to an object, it cannot be changed to refer to another object." But does this only apply locally. The following codes can compile successfully inside function which is controdict with the rule
int x; int y; int& r = x; r = y;
----------- Another question
extern class A;
void h(A*& a)
{
...
}
int main() {
A a;
h(&a); // Error ,Line# 2
A *b = New A;
h(b) // CorrectWhy line 2 can't pass the compilation? I see no difference between "&a" and "b"
-
It is said that "Once a reference is initialized to an object, it cannot be changed to refer to another object." But does this only apply locally. The following codes can compile successfully inside function which is controdict with the rule
int x; int y; int& r = x; r = y;
----------- Another question
extern class A;
void h(A*& a)
{
...
}
int main() {
A a;
h(&a); // Error ,Line# 2
A *b = New A;
h(b) // CorrectWhy line 2 can't pass the compilation? I see no difference between "&a" and "b"
There's no contradiction. Check this modified code
int x = 10; int y = 20; int& r = x; // make r refer to x r = y; // this does not change the reference, value of y is copied to r, i.e., to x r += 10; // this will modify x, not y, because r still refers to x, not y cout << r << " " << x << " " << y ; // prints 30 30 20
For the second one, you have created reference to a pointer in the function parameterA*& a
. By this it meansa
refers to a pointer of typeA
which it can modify. However when you pass&a
to the functionh
,&a
is not modifiable. Check this sample codeA a1, a2; A* p1; A* &r1 = p1; // Ok, r1 refers to a modifiable pointer p1 p1 = &a1; // p1 points to a1 r1 = &a2; // now p1 points to a2 A* &r2 = &a2; // ERROR: &a2 is not modifiable, &a2 is a constant pointer
-
There's no contradiction. Check this modified code
int x = 10; int y = 20; int& r = x; // make r refer to x r = y; // this does not change the reference, value of y is copied to r, i.e., to x r += 10; // this will modify x, not y, because r still refers to x, not y cout << r << " " << x << " " << y ; // prints 30 30 20
For the second one, you have created reference to a pointer in the function parameterA*& a
. By this it meansa
refers to a pointer of typeA
which it can modify. However when you pass&a
to the functionh
,&a
is not modifiable. Check this sample codeA a1, a2; A* p1; A* &r1 = p1; // Ok, r1 refers to a modifiable pointer p1 p1 = &a1; // p1 points to a1 r1 = &a2; // now p1 points to a2 A* &r2 = &a2; // ERROR: &a2 is not modifiable, &a2 is a constant pointer
I see your point.
int x = 3;
int y = 1;
int& r = x;
r = y;int main() {}
The above codes can't pass the compilation and with a error message "'r' : missing storage-class or type specifiers", It doesn't make sense to me, here the last line just assign "y" to the object "r" refers which is "x", and when I move it to local function the error disappears, why?
-
I see your point.
int x = 3;
int y = 1;
int& r = x;
r = y;int main() {}
The above codes can't pass the compilation and with a error message "'r' : missing storage-class or type specifiers", It doesn't make sense to me, here the last line just assign "y" to the object "r" refers which is "x", and when I move it to local function the error disappears, why?
YongSheng Li wrote: int x = 3; int y = 1; int& r = x; r = y; int main() {} The first three lines are variable declarations that include their initialization. That's valid at the file scope. The fourth line (
r = y;
) is an assignment, which is not legal at the file scope; it's only valid in the body of a function. -- jlr http://jlamas.blogspot.com/[^] -
I see your point.
int x = 3;
int y = 1;
int& r = x;
r = y;int main() {}
The above codes can't pass the compilation and with a error message "'r' : missing storage-class or type specifiers", It doesn't make sense to me, here the last line just assign "y" to the object "r" refers which is "x", and when I move it to local function the error disappears, why?
You can only declare and initialize variables outside of functions, you can't write statements like
r=y
. Regards Senthil _____________________________ My Blog | My Articles | WinMacro -
YongSheng Li wrote: int x = 3; int y = 1; int& r = x; r = y; int main() {} The first three lines are variable declarations that include their initialization. That's valid at the file scope. The fourth line (
r = y;
) is an assignment, which is not legal at the file scope; it's only valid in the body of a function. -- jlr http://jlamas.blogspot.com/[^]Beat me to it by a minute :) Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
Beat me to it by a minute :) Regards Senthil _____________________________ My Blog | My Articles | WinMacro
It's ok. I'll share the prize with you :) -- jlr http://jlamas.blogspot.com/[^]
-
Beat me to it by a minute :) Regards Senthil _____________________________ My Blog | My Articles | WinMacro