由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - which style do you prefer?
相关主题
老年工程师关于std::vector的几个问题问个overloading new operator的问题
why copy assignment operator returns non-const type?question overloading ++ error
不用头文件,如何调用函数?C++请问关于overloading <<
请教一个class design的问题function declaration
An object of A automatically converted to an object of B.post increment
warning: returning address of local variable or temporaryis this behavior undefined?
why use static function here?有关objec access path的问题
question about const referenceC++ 的 问题
相关话题的讨论汇总
话题: int话题: vector话题: res话题: object话题: return
进入Programming版参与讨论
1 (共1页)
g*********s
发帖数: 1782
1
vector& f(int x) {
vector res;
for ( int i = 0; i < x; ++ i )
res.push_back(i);
return res;
}
void f(int x, vector& res) {
for ( int i = 0; i < x; ++ i )
res.push_back(i);
}
X****r
发帖数: 3557
2
The former is wrong.

【在 g*********s 的大作中提到】
: vector& f(int x) {
: vector res;
: for ( int i = 0; i < x; ++ i )
: res.push_back(i);
: return res;
: }
: void f(int x, vector& res) {
: for ( int i = 0; i < x; ++ i )
: res.push_back(i);
: }

e****d
发帖数: 895
3
Do you mean to return vector instead of vector&
in the first case? Otherwise, you should const-qualify
it.

【在 g*********s 的大作中提到】
: vector& f(int x) {
: vector res;
: for ( int i = 0; i < x; ++ i )
: res.push_back(i);
: return res;
: }
: void f(int x, vector& res) {
: for ( int i = 0; i < x; ++ i )
: res.push_back(i);
: }

t****t
发帖数: 6806
4
returning a const& to local object is wrong too...

【在 e****d 的大作中提到】
: Do you mean to return vector instead of vector&
: in the first case? Otherwise, you should const-qualify
: it.

g*********s
发帖数: 1782
5
what if i need to return the reference for convenience?

【在 t****t 的大作中提到】
: returning a const& to local object is wrong too...
y***d
发帖数: 2330
6
then it is a trouble for the future.

【在 g*********s 的大作中提到】
: what if i need to return the reference for convenience?
S**I
发帖数: 15689
7
you can't do this, it's just wrong.

【在 g*********s 的大作中提到】
: what if i need to return the reference for convenience?
e****d
发帖数: 895
8
That's right. Should not return reference to local object.
But are the following two cases valid?
extern const vector & foo(); //return reference to local obj.
case 1,
vector v(foo());
case 2,
foo().size();

【在 t****t 的大作中提到】
: returning a const& to local object is wrong too...
z****e
发帖数: 2024
9
师傅给展开讲讲吧。这个我感觉很多地方都用。

【在 t****t 的大作中提到】
: returning a const& to local object is wrong too...
S**I
发帖数: 15689
10
could be valid if the vector is not a local object created within foo().

【在 e****d 的大作中提到】
: That's right. Should not return reference to local object.
: But are the following two cases valid?
: extern const vector & foo(); //return reference to local obj.
: case 1,
: vector v(foo());
: case 2,
: foo().size();

相关主题
warning: returning address of local variable or temporary问个overloading new operator的问题
why use static function here?question overloading ++ error
question about const reference请问关于overloading <<
进入Programming版参与讨论
t****t
发帖数: 6806
11
哪里用?

【在 z****e 的大作中提到】
: 师傅给展开讲讲吧。这个我感觉很多地方都用。
t****t
发帖数: 6806
12
no, they are all undefined.

【在 e****d 的大作中提到】
: That's right. Should not return reference to local object.
: But are the following two cases valid?
: extern const vector & foo(); //return reference to local obj.
: case 1,
: vector v(foo());
: case 2,
: foo().size();

z****e
发帖数: 2024
13
比如op[]的一个overload的const版本,就有人返回const&
member function 返回const&的是不是不算是你说得case?

【在 t****t 的大作中提到】
: 哪里用?
p***o
发帖数: 1252
14
That's not a reference to a LOCAL variable.

【在 z****e 的大作中提到】
: 比如op[]的一个overload的const版本,就有人返回const&
: member function 返回const&的是不是不算是你说得case?

z****e
发帖数: 2024
15
是的,但可以在stack上面。原object销毁的时候咋办。

【在 p***o 的大作中提到】
: That's not a reference to a LOCAL variable.
t****t
发帖数: 6806
16
almost any object can be on stack.

【在 z****e 的大作中提到】
: 是的,但可以在stack上面。原object销毁的时候咋办。
e****d
发帖数: 895
17
You should not access any non-static data member of an
object that has been destroyed.

【在 z****e 的大作中提到】
: 是的,但可以在stack上面。原object销毁的时候咋办。
p***o
发帖数: 1252
18
I don't get your point. Show your code.

【在 z****e 的大作中提到】
: 是的,但可以在stack上面。原object销毁的时候咋办。
d****p
发帖数: 685
19
This case is different.
The object associated with the returned reference is on a stack with longer
lifecycle or from heap.

【在 z****e 的大作中提到】
: 比如op[]的一个overload的const版本,就有人返回const&
: member function 返回const&的是不是不算是你说得case?

z****e
发帖数: 2024
20
大师也多日不见了。
偶新年得了个offer,可惜公司后来说高层人事调整,offer又被收回。
我真是服了。

longer

【在 d****p 的大作中提到】
: This case is different.
: The object associated with the returned reference is on a stack with longer
: lifecycle or from heap.

相关主题
function declaration有关objec access path的问题
post incrementC++ 的 问题
is this behavior undefined?[合集] 请问-fno-implicit-templates的用处
进入Programming版参与讨论
h***i
发帖数: 1970
21
return vector就行,编译器自动优化。

【在 g*********s 的大作中提到】
: vector& f(int x) {
: vector res;
: for ( int i = 0; i < x; ++ i )
: res.push_back(i);
: return res;
: }
: void f(int x, vector& res) {
: for ( int i = 0; i < x; ++ i )
: res.push_back(i);
: }

e****d
发帖数: 895
22
RVO will apply when the returned value is used
to construct another vector.
Or wait until the move semantics has been implemented in stl.

【在 h***i 的大作中提到】
: return vector就行,编译器自动优化。
t****t
发帖数: 6806
23
move semantics has been implemented in stl for gcc.

【在 e****d 的大作中提到】
: RVO will apply when the returned value is used
: to construct another vector.
: Or wait until the move semantics has been implemented in stl.

g*********s
发帖数: 1782
24
sorry, u guys mean the following won't trigger an extra vector copy?
vector f(int x) {
vector res;
for ( int i = 0; i < x; ++ i )
res.push_back(i);
return res;
}

【在 t****t 的大作中提到】
: move semantics has been implemented in stl for gcc.
e****d
发帖数: 895
25
vector<> of c++0x stl will contain move constructor / move
assignment operator.

【在 g*********s 的大作中提到】
: sorry, u guys mean the following won't trigger an extra vector copy?
: vector f(int x) {
: vector res;
: for ( int i = 0; i < x; ++ i )
: res.push_back(i);
: return res;
: }

1 (共1页)
进入Programming版参与讨论
相关主题
C++ 的 问题An object of A automatically converted to an object of B.
[合集] 请问-fno-implicit-templates的用处warning: returning address of local variable or temporary
(面试题) 给code挑毛病(updated with multiple choices)why use static function here?
conversion between const to nonconstquestion about const reference
老年工程师关于std::vector的几个问题问个overloading new operator的问题
why copy assignment operator returns non-const type?question overloading ++ error
不用头文件,如何调用函数?C++请问关于overloading <<
请教一个class design的问题function declaration
相关话题的讨论汇总
话题: int话题: vector话题: res话题: object话题: return