方法如何返回对象 [英] How Methods Return Objects

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

问题描述

我想知道当方法返回

对象时引擎盖下发生了什么。例如,我有一个简单的Point类,有两个成员_x和_y。

它是构造函数,复制构造函数,赋值运算符和additon

运算符(返回另一个Point)对象,我的问题是

左右)如下:


Point :: Point(int x,int y):

_x(x),

_y(y){}


Point :: Point(const Point& p){

_x = p._x;

_y = p._y;

}


Point& Point :: operator =(const Point& p){

_x = p._x;

_y = p._y;

return( * this);

}


Point Point :: operator +(const Point& p)const {

Point ret(_x + p._x,_y + p._y);

返回ret;

}


当operator +返回时会发生什么
创建的临时Point对象?

是否使用复制构造函数创建临时

变量的副本;保留" ;?之后会被销毁吗?


如果没有,它如何返回Point对象?


另外,为什么这个新对象?从来没有发挥过?编译器如何预先知道
将通过operator +创建多少Point对象?

是否因为此类对象必须存在于以下语句中:


点p3 = p1 + p2;


我知道这是一个简单的观点,但我很困惑。


感谢您的任何澄清,

cpp

I''d like to know what goes on under the hood when methods return
objects. Eg, I have a simple Point class with two members _x and _y.
It''s constructor, copy constructor, assignment operator and additon
operator (which returns another Point object, and which my question is
about) are as follows:

Point::Point(int x, int y) :
_x(x),
_y(y) { }

Point::Point(const Point& p) {
_x = p._x;
_y = p._y;
}

Point& Point::operator=(const Point& p) {
_x = p._x;
_y = p._y;
return (*this);
}

Point Point::operator+(const Point& p) const {
Point ret(_x + p._x, _y + p._y);
return ret;
}

What happens when operator+ returns the temporary Point object that it
creates?

Does it use the copy constructor to create a copy of the temporary
variable "ret"? Is ret destroyed after that?

If not, how does it return a Point object?

Also, why is that "new" never comes into play? How can the compiler
know beforehand how many Point objects will be created via operator+?
Is it because such objects must exist in statements like:

Point p3 = p1 + p2;

I know this is a simple point, but I''m confused nonetheless.

Thanks for any clarification,
cpp

推荐答案

" cppaddict" <他*** @ hello.com>写了...
"cppaddict" <he***@hello.com> wrote...
我想知道当方法返回
对象时引擎盖下发生了什么。例如,我有一个简单的Point类,有两个成员_x和_y。
它是构造函数,复制构造函数,赋值运算符和additon
运算符(返回另一个Point对象,我的问题是< br:> about)如下:

Point :: Point(int x,int y):
_x(x),
_y(y){}
_x = p._x;
_y = p._y;


我想知道你为什么不在这里使用初始化...

}

点& Point :: operator =(const Point& p){
_x = p._x;
_y = p._y;
return(* this);
}
点Point :: operator +(const Point& p)const {
Point ret(_x + p._x,_y + p._y);
返回ret;
}

当operator +返回它创建的临时Point对象时会发生什么?

它是否使用复制构造函数来创建临时的副本
变量ret?那之后被摧毁了吗?


一般来说,是的。由于''ret''是一个自动变量,它将在函数结束后立即销毁(在''return''

语句之后)。为了使Point对象在函数

完成之后但在其他地方使用其值之前存在,使用复制构造函数创建临时的



如果没有,它如何返回Point对象?

另外,为什么这个新对象?从来没有发挥过?


你怎么知道它不是?使用某种特殊形式的新来创建一个临时_might_内部

。它没有定义,AFAIK。

临时创建的地方,以及如何,不重要。从langauge的角度来看,至少



编译器如何预先知道将通过operator +创建多少Point对象?


每次调用operator +函数时,可能会创建另一个Point对象



是因为这样的对象必须存在于以下语句中:

点p3 = p1 + p2;
I''d like to know what goes on under the hood when methods return
objects. Eg, I have a simple Point class with two members _x and _y.
It''s constructor, copy constructor, assignment operator and additon
operator (which returns another Point object, and which my question is
about) are as follows:

Point::Point(int x, int y) :
_x(x),
_y(y) { }

Point::Point(const Point& p) {
_x = p._x;
_y = p._y;
I wonder why you didn''t use initialisation here...
}

Point& Point::operator=(const Point& p) {
_x = p._x;
_y = p._y;
return (*this);
}

Point Point::operator+(const Point& p) const {
Point ret(_x + p._x, _y + p._y);
return ret;
}

What happens when operator+ returns the temporary Point object that it
creates?

Does it use the copy constructor to create a copy of the temporary
variable "ret"? Is ret destroyed after that?
Generally speaking, yes. Since ''ret'' is an automatic variable, it will
be destroyed right after the function ends (right after the ''return''
statement). In order for the Point object to exist after the function
has finished but before its value is used elsewhere, a temporary one
is created using the copy constructor.

If not, how does it return a Point object?

Also, why is that "new" never comes into play?
How do you know it doesn''t? Creation of a temporary _might_ be internally
done using some kind of special form of ''new''. It''s not defined, AFAIK.
Where the temporary is created and how is, well, unimportant. At least
from the langauge standpoint.
How can the compiler
know beforehand how many Point objects will be created via operator+?
Every time it has to call operator+ function, another Point object is
potentially created.
Is it because such objects must exist in statements like:

Point p3 = p1 + p2;




在这种情况下,允许编译器放弃创建临时文件

而是生成代码,以便当您从

函数运算符+返回''ret''时,它用于直接初始化''p3''对象。 br />

你可能会在好书中找到所有这些。或者通过查看编译器从您的

C ++源代码生成的代码(是的,

它通常是汇编语言)。


Victor



The compiler is allowed to forgo creation of a temporary in this case
and instead generate code so that when you return the ''ret'' from the
function operator+, it is used to directly initialise the ''p3'' object.

You might find all that in good books. Or by looking at the code (yes,
it''s usually assembly language) that the compiler generates from your
C++ source.

Victor


Point :: Point(const Point& p){
_x = p._x;
_y = p._y;
Point::Point(const Point& p) {
_x = p._x;
_y = p._y;



我想知道你为什么不在这里使用初始化...



I wonder why you didn''t use initialisation here...



我应该有。感谢您指出这一点。


感谢您的其他解释。


cpp



I should have. Thanks for pointing that out.

Thanks for you other explanations as well.

cpp



cppaddict写道:

cppaddict wrote:
Point :: Point(const Point& amp ; p){
_x = p._x;
_y = p._y;
Point::Point(const Point& p) {
_x = p._x;
_y = p._y;




它是在变量名称中使用前导下划线是不明智的,尽管在这种情况下,这不是技术上的非法(17.4.3.1.2)。


mark



it is unwise to use a leading underscore in a variable name, although in
this case, not technically illegal (17.4.3.1.2).

mark


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

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