函数返回本地对象??? [英] Function Returning a local object???

查看:54
本文介绍了函数返回本地对象???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Nigel Chapman的C ++深夜指南,我认为这是一本绝对精彩的书。但是在第175页(主题:运算符

重叠),有以下代码片段:


内联MFVec运算符+(const MFVec& z1,const MFVec& z2 )//全球

功能

{

MFVec res = z1;

res + = z2

返回res; //为什么???

}


MFVec是(矢量)类:


类MFVec {

public:

MFVec(float x,float y);

//其他会员功能

private:

浮动xcoord,ycoord;

}


然而我的问题在于全局运算符的第3行+()

函数,因为它返回局部变量res,我假设退出函数后会立即销毁

。这类似于悬挂

refrence,我不知道为什么它不违法。谁知道为什么?


我不介意彻底的答案;事实上我的孩子更喜欢他们;-)


谢谢


- Olumide

I''m studying Nigel Chapman''s Late Night Guide to C++ which I think is
an absolutely fantastic book; however on page 175 (topic: operator
overlaoding), there the following code snippet:

inline MFVec operator+(const MFVec& z1, const MFVec& z2) // Global
function
{
MFVec res = z1;
res += z2
return res; // WHY???
}

MFVec is the (vector) class:

class MFVec {
public:
MFVec(float x, float y);
// other member functions
private:
float xcoord, ycoord;
}

My problem however lies with the line 3 of the global operator+()
function because it returns the local variable res, which I assume is
destroyed as soon as the function is exited. This resembles a dangling
refrence and I don''t know why its not illegal. Anyone knows why?

I don''t mind thorough answers; in fact I kidda prefer them ;-)

Thanks

- Olumide

推荐答案



" Olumide" < 50 *** @ web.de>在消息中写道

news:c8 ************************** @ posting.google.c om ...

"Olumide" <50***@web.de> wrote in message
news:c8**************************@posting.google.c om...
我正在研究Nigel Chapman的C ++深夜指南,我认为这是一本绝对精彩的书;但是在第175页(主题:运算符
重叠),有以下代码片段:

内联MFVec运算符+(const MFVec& z1,const MFVec& z2)// Global
功能
{MFVec res = z1;
res + = z2
返回res; //为什么???
}

MFVec是(矢量)类:

类MFVec {
公众:
MFVec(浮动x,浮动y);
//其他成员函数
私有:
浮动xcoord,ycoord;
}

然而我的问题在于全局运算符的第3行+()
函数,因为它返回局部变量res,我假设退出函数后会立即销毁它。这类似于悬挂式的反思,我不知道为什么它不违法。谁知道为什么?

我不介意彻底的答案;事实上我的孩子更喜欢他们;-)

- Olumide
I''m studying Nigel Chapman''s Late Night Guide to C++ which I think is
an absolutely fantastic book; however on page 175 (topic: operator
overlaoding), there the following code snippet:

inline MFVec operator+(const MFVec& z1, const MFVec& z2) // Global
function
{
MFVec res = z1;
res += z2
return res; // WHY???
}

MFVec is the (vector) class:

class MFVec {
public:
MFVec(float x, float y);
// other member functions
private:
float xcoord, ycoord;
}

My problem however lies with the line 3 of the global operator+()
function because it returns the local variable res, which I assume is
destroyed as soon as the function is exited. This resembles a dangling
refrence and I don''t know why its not illegal. Anyone knows why?

I don''t mind thorough answers; in fact I kidda prefer them ;-)

Thanks

- Olumide




代码很好。它不会返回本地,而是一份副本。返回

类型是类,而不是参考。



The code is fine. It is not returning the local, but a copy. The return
type is the class, not a reference.




" Olumide" < 50 *** @ web.de>在消息中写道

news:c8 ************************** @ posting.google.c om ...

"Olumide" <50***@web.de> wrote in message
news:c8**************************@posting.google.c om...
我正在研究Nigel Chapman的C ++深夜指南,我认为这是一本绝对精彩的书;但是在第175页(主题:运算符
重叠),有以下代码片段:

内联MFVec运算符+(const MFVec& z1,const MFVec& z2)// Global
功能
{MFVec res = z1;
res + = z2
返回res; //为什么???
}

MFVec是(矢量)类:

类MFVec {
公众:
MFVec(浮动x,浮动y);
//其他成员函数
私有:
浮动xcoord,ycoord;
}

然而我的问题在于全局运算符的第3行+()
函数,因为它返回局部变量res,我假设退出函数后会立即销毁它。这类似于悬挂式的反思,我不知道为什么它不违法。谁知道为什么?

我不介意彻底的答案;事实上我的孩子更喜欢他们;-)
谢谢


不,这个代码是正义的,它可以返回一个对象,虽然对象

" res"将

销毁它将被复制并将副本返还给您(但是

编译器

将通常优化这个序列并将实际的res返回给你并且

不会破坏它。)

你引用的例子是在使用的模式的意义上的惯用语/>


实现定义+操作的类,如复数,

向量等。

你可以检查通过查看this,用于res的指针对象和

返回的

对象,它们应该是不同的(或者至少在调试编译模式下)。


你可能会混淆返回对本地对象的引用。这是纯粹的

邪恶...

一旦函数返回值,对象就会被销毁,你就是

挂在

a无效参考。例如:


foo& getfoo(){foo f;返回f;}

....

foo& g = getfoo(); // f现在不再是一个有效的对象,所以g不是

有效的引用...


你可以在构造函数中放入一些调试printfs ,destructors,和你

应该

看到你回来的参考已经被烤了。


有时候你会看到返回类的成员函数

引用对象,当有问题的对象是数据时,这是可以的



对象的成员 - 这些不是本地对象,因为它们存在的时间是

拥有它们所属的
对象 - 尽管这种方法在

对象时有问题本身可以重新定位

并销毁/无效(例如在STL容器类中)。在这种情况下你可能需要在b / b
无效之前立即复制或处理返回值。


dave

- Olumide
I''m studying Nigel Chapman''s Late Night Guide to C++ which I think is
an absolutely fantastic book; however on page 175 (topic: operator
overlaoding), there the following code snippet:

inline MFVec operator+(const MFVec& z1, const MFVec& z2) // Global
function
{
MFVec res = z1;
res += z2
return res; // WHY???
}

MFVec is the (vector) class:

class MFVec {
public:
MFVec(float x, float y);
// other member functions
private:
float xcoord, ycoord;
}

My problem however lies with the line 3 of the global operator+()
function because it returns the local variable res, which I assume is
destroyed as soon as the function is exited. This resembles a dangling
refrence and I don''t know why its not illegal. Anyone knows why?

I don''t mind thorough answers; in fact I kidda prefer them ;-)

Thanks
No, this code is righteous, its ok to return an object, although the object
"res"will
be destroyed it will be copied and the copy returned to you ( but
compilers
will generally optimize this sequence and return the actual res to you and
not destroy it).
The example you quote is idiomatic in the sense that it is the pattern used
to
implement classes which define the + operation such as complex numbers,
vectors, etc.
You can check this by looking at the "this" pointer for the "res" object and
the returned
object, they should be different ( or at least in debug compile mode).

You might be confusing returning references to local objects. This is pure
evil...
once the function returns the value, the object is destroyed and you are
hanging onto
a invalid reference. For instance :

foo& getfoo(){ foo f; return f;}
....
foo& g = getfoo(); // f is not a valid object anymore now so g is not a
valid reference...

You can put in some debug printfs in the constructors, destructors, and you
should
see that the reference you are getting back is already toasted.

Sometimes you will see member functions of a class returning
references to objects, this is ok when the objects in question are data
members of the
object - these are not local objects because they exist for the lifetime of
the owning
object they belong to - although this approach is questionable when the
object itself may be relocated
and destroyed/invalidated ( such as in STL container classes). In such
circumstances you might
need to copy or process the return value immediately before its destroyed or
invalidated.

dave


- Olumide



Olumide写道:
Olumide wrote:
我正在研究奈杰尔·查普曼的C ++深夜指南,我认为这本书绝对精彩;但是,在第175页,
(主题:运算符重载),有以下代码片段:

内联
MFVec运算符+(const MFVec& z1,const MFVec& z2){
// Globalfunction
MFVec res = z1;
res + = z2
返回res; //为什么???
}

MFVec是(矢量)类:

类MFVec {
公众:
MFVec(浮动x,浮动y);
//其他会员功能
私人:
浮动xcoord,ycoord;
};

然而我的问题在于全局运算符的第3行+()
函数,因为它返回局部变量res,
我假设函数退出后会立即销毁。
这类似于悬空的参考任何人都知道为什么?
I''m studying Nigel Chapman''s Late Night Guide to C++
which I think is an absolutely fantastic book; however, on page 175,
(topic: operator overloading), there the following code snippet:

inline
MFVec operator+(const MFVec& z1, const MFVec& z2) {
// Globalfunction
MFVec res = z1;
res += z2
return res; // WHY???
}

MFVec is the (vector) class:

class MFVec {
public:
MFVec(float x, float y);
// other member functions
private:
float xcoord, ycoord;
};

My problem however lies with the line 3 of the global operator+()
function because it returns the local variable res,
which I assume is destroyed as soon as the function is exited.
This resembles a dangling refrence
and I don''t know why its not illegal.
Anyone knows why?




operator +(const MFVec&,const MFVec&)

返回*值*而不是*引用*。

要返回引用,您必须写:


inline
MFVec& operator +(const MFVec& z1,const MFVec& z2){

//全局函数

MFVec res = z1;

res + = z2

返回res; //坏主意!

}



operator+(const MFVec&, const MFVec&)
returns a *value* not a *reference*.
To return a reference, you must write:

inline
MFVec& operator+(const MFVec& z1, const MFVec& z2) {
// Global function
MFVec res = z1;
res += z2
return res; // Bad idea!
}


这篇关于函数返回本地对象???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆