由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - how do I reseat a reference?
相关主题
question for C++ constantmember and friend
C++ 的 问题One c++ non-type template question
一个C++语法问题g++ default optimization error
question about c++ constructor关于C++ STL编译的疑问
private destructorC++ optimization question
请问这个C++程序有什么问题吗关于Makefile的一个问题
问一个for循环的问题What is wrong in this array declaration.
namespace defined in another file问个虚函数的作用
相关话题的讨论汇总
话题: reference话题: std话题: object话题: reseat话题: ref
进入Programming版参与讨论
1 (共1页)
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.

1 (共1页)
进入Programming版参与讨论
相关主题
问个虚函数的作用private destructor
C++ template problem请问这个C++程序有什么问题吗
请教C++中的unordered_set问一个for循环的问题
C++ questionnamespace defined in another file
question for C++ constantmember and friend
C++ 的 问题One c++ non-type template question
一个C++语法问题g++ default optimization error
question about c++ constructor关于C++ STL编译的疑问
相关话题的讨论汇总
话题: reference话题: std话题: object话题: reseat话题: ref