p*u 发帖数: 2454 | 1 I know a reference in C++ cannot be reseated; am just trying to test
this concept. But it seems to me that as long as u wish to implement an
assignment operator u will not get a "reference reseating" compiler
error, it will just copy value of second object to first object. How can
I have a true compiler error?
Sample code:
"
#include
#include
int main()
{
std::vector v1( 10, 1 );
std::vector v2( 20, 2 );
std::vector& ref = v1;
for( int i = 0; i < ref.size(); ++i )
{
std::cout << i << "th=" << ref[i] << "\t";
}
std::cout << std::endl;
ref = v2;
for( int i = 0; i < ref.size(); ++i )
{
std::cout << i << "th=" << ref[i] << "\t";
}
std::cout << std::endl;
}
" |
X****r 发帖数: 3557 | 2 I never heard of this "reference reseating" compiler error. AFAIK
there is just no syntax to tell the compiler that you want to reseat
a reference.
【在 p*u 的大作中提到】 : I know a reference in C++ cannot be reseated; am just trying to test : this concept. But it seems to me that as long as u wish to implement an : assignment operator u will not get a "reference reseating" compiler : error, it will just copy value of second object to first object. How can : I have a true compiler error? : Sample code: : " : #include : #include : int main()
|
p*u 发帖数: 2454 | 3
then where does this "reference reseating" come from? in another word, why
should people know u cannot reseat a reference when u can't do such a
thing?
【在 X****r 的大作中提到】 : I never heard of this "reference reseating" compiler error. AFAIK : there is just no syntax to tell the compiler that you want to reseat : a reference.
|
X****r 发帖数: 3557 | 4 I'm not the one who raised this question so I have no idea where this
"reference reseating" come from. In fact, this is the first time I
ever hear of this term.
why
【在 p*u 的大作中提到】 : : then where does this "reference reseating" come from? in another word, why : should people know u cannot reseat a reference when u can't do such a : thing?
|
p***o 发帖数: 1252 | 5 [31.1] What is value and/or reference semantics, and which is best in C++?
http://www.parashift.com/c++-faq-lite/value-vs-ref-semantics.html
【在 p*u 的大作中提到】 : : then where does this "reference reseating" come from? in another word, why : should people know u cannot reseat a reference when u can't do such a : thing?
|
d****p 发帖数: 685 | 6 reference is
1. a pointer
2. a pointer to a valid object
3. a pointer which is always associated with the valid object it points to
When you try to reseat a reference, you are breaking rule 3. If you want sth
that could be reseatable, use a
pointer.
Syntactically a reference is "reseated" since you are actually assigning the
new object to the object the
reference was originally bound to. In your case, ref = v2 is actually v1 =
v2
reference is a concept manipulated by compiler. Its physical presence in
generated code is pointer or other
forms related to optimization.
【在 p*u 的大作中提到】 : : then where does this "reference reseating" come from? in another word, why : should people know u cannot reseat a reference when u can't do such a : thing?
|
b********n 发帖数: 609 | 7 "a reference cannot be reseated"是指当ref指向v1之后,你只能通过它来对v1进行操
作,而不能通过"ref=v2"之后对v2进行操作,ref=v2就是copy。
实际上你是没有办法写code来命令ref指向v2,然后指望编译器报错的。
【在 p*u 的大作中提到】 : I know a reference in C++ cannot be reseated; am just trying to test : this concept. But it seems to me that as long as u wish to implement an : assignment operator u will not get a "reference reseating" compiler : error, it will just copy value of second object to first object. How can : I have a true compiler error? : Sample code: : " : #include : #include : int main()
|
b********n 发帖数: 609 | 8 看来你很懂啊,看看这段code,算不算reseating:
“
object& operator=( const object& rhs )
{
if ( this != &rhs )
{
this->~object();
new ( this ) object( rhs );
}
return *this;
}
”
sth
the
【在 d****p 的大作中提到】 : reference is : 1. a pointer : 2. a pointer to a valid object : 3. a pointer which is always associated with the valid object it points to : When you try to reseat a reference, you are breaking rule 3. If you want sth : that could be reseatable, use a : pointer. : Syntactically a reference is "reseated" since you are actually assigning the : new object to the object the : reference was originally bound to. In your case, ref = v2 is actually v1 =
|
C**m 发帖数: 126 | 9 no
【在 b********n 的大作中提到】 : 看来你很懂啊,看看这段code,算不算reseating: : “ : object& operator=( const object& rhs ) : { : if ( this != &rhs ) : { : this->~object(); : new ( this ) object( rhs ); : } : return *this;
|
k******r 发帖数: 2300 | 10 You can call yourself John today and Mike tomorrow and Jack next month. No
matter how many names you call yourself, you are nobody but yourself. Nobody
will blame you you have so many names(at least legally you are fine). The
same moral philosophy applies here. The compiler won't complain a variable
has so many nick names(namely alias) because a variable is not identified by
name.
【在 p*u 的大作中提到】 : I know a reference in C++ cannot be reseated; am just trying to test : this concept. But it seems to me that as long as u wish to implement an : assignment operator u will not get a "reference reseating" compiler : error, it will just copy value of second object to first object. How can : I have a true compiler error? : Sample code: : " : #include : #include : int main()
|
d****p 发帖数: 685 | 11 I like this explanation :-)
And one more thing to add up, John is always YOUR name even you have new
fancier new names and have
forgot your first name as John - John is loyal and always belongs to you.
Once you had it, you will always have
it. That's so cool.
Nobody
by
【在 k******r 的大作中提到】 : You can call yourself John today and Mike tomorrow and Jack next month. No : matter how many names you call yourself, you are nobody but yourself. Nobody : will blame you you have so many names(at least legally you are fine). The : same moral philosophy applies here. The compiler won't complain a variable : has so many nick names(namely alias) because a variable is not identified by : name.
|