boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 被reference搞晕了
相关主题
const object
请问个c++ primer里面的小白问题.
const reference in copy constructor
一个c++ constructor的问题, thanks
c++ question
what is the difference?
大家觉得C++复杂在哪里?
请教一下,exception时,destructor一定会被调用么?
一个 default constructor 的问题
What does the default constructor do?
相关话题的讨论汇总
话题: reference话题: case话题: return话题: fn话题: value
进入Programming版参与讨论
1 (共1页)
d*******n
发帖数: 369
1
以下2种case,哪些对?
// use template for generality
case 1
T fn(T & in)
{
T &value = in; //two references point to one object (ok?)
// do something
return value; //is a reference returned?
}
case 2
T &fn(T & in)
{
T &value = in;
// do something
return value; //is a reference returned? what difference from the case 1?
}
My purpose: if class T is big, it's efficient to pass/return its reference.
The above 2 cases will do?
c********e
发帖数: 383
2
IMHO, both case 1 and 2 are correct c++ code.
for case 1 when the reference is returned, it is made into
a temporary object first, which is the semantic of return by value.
for all POD and user defined types, there must be a copy constructor
which takes a reference type and return (not really return, but contruction)
a object of that type. which is then returned to the caller.
so ur case 1 fn semantic is: (assuming do something instead of do nothing
in your fn definition)
change the object passe

【在 d*******n 的大作中提到】
: 以下2种case,哪些对?
: // use template for generality
: case 1
: T fn(T & in)
: {
: T &value = in; //two references point to one object (ok?)
: // do something
: return value; //is a reference returned?
: }
: case 2

c********e
发帖数: 383
3
IMHO, both case 1 and 2 are correct c++ code.
for case 1 when the reference is returned, it is made into
a temporary object first, which is the semantic of return by value.
for all POD and user defined types, there must be a copy constructor
which takes a reference type and return (not really return, but contruction)
a object of that type. which is then returned to the caller.
so ur case 1 fn semantic is: (assuming do something instead of do nothing
in your fn definition)
change the object passe

【在 d*******n 的大作中提到】
: 以下2种case,哪些对?
: // use template for generality
: case 1
: T fn(T & in)
: {
: T &value = in; //two references point to one object (ok?)
: // do something
: return value; //is a reference returned?
: }
: case 2

1 (共1页)
进入Programming版参与讨论
相关主题
What does the default constructor do?
which style do you prefer?
How to initialize object in constructor?
C++ questions
copy constructor问题。
内存泄露了吗?
C++编程原则的问题
question regarding const function
《新C++标准:C++0x 概述》英文文字版[PDF]
问一个constructor的问题 (转载)
相关话题的讨论汇总
话题: reference话题: case话题: return话题: fn话题: value