C ++编译器“返回”行为(大师问题;) [英] C++ compiler "return" behavior (guru question ;)

查看:84
本文介绍了C ++编译器“返回”行为(大师问题;)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


如果我从一个函数返回一个类

对象,我试图了解会发生什么。

看来C ++(MinGW)没有调用复制构造函数,如果我这样做:
这样的东西:


SomeObj foo()

{

SomeObj X;

// ....

返回X;

}


但它每次调用函数时都会创建一个新对象,并且

看起来这些对象似乎是可用的(我创建了一个小测试

程序检查这个:)。

基本上我可以进入main:


int main()

{

// ....

SomeObj bar = foo();

SomeObj bar2 = foo();

//由于一些奇怪的原因,接下来的事情会失败,但是:

SomeObj& bar3 = foo();

}


我会得到两个不同的物品,两者都有效。 (正如评论所说,

第三个会因为临时对象的一些模糊警告而失败 -

但是没有发生复制,所以没有临时对象,AFAIK)。

嗯。


所以我查了一下其他东西:


void foobar()

{

SomeObj IamUseless;

}


该对象实际构建,但在函数后被销毁。

现在我有点不确定C ++编译器如何确定每个时间

哪个对象被返回(可能是在STL容器的深度

无论如何),哪一个没有,所以这必须被销毁。


通常我使用new()用于工厂,但我想知道,我检查了。而我

不能得到它:)。所以也许在这里有一位大师可以帮我解决一些背景信息吗? ;)

干杯&谢谢,

Axel。

Hi all,

I am trying to get my head around what happens if I return a class
object from a function.
It seems C++ (MinGW) does not invoke the copy constructor if I do
something like this:

SomeObj foo()
{
SomeObj X;
// ....
return X;
}

BUT it does create a new object every time the function is invoked, and
it does seem like these objects are usable (I created a little test
program checking this :).
So basically I can go in main:

int main()
{
// ....
SomeObj bar = foo();
SomeObj bar2 = foo();
// for some strange reason the next thing will fail, though:
SomeObj &bar3 = foo();
}

and I will get two different objects back, both valid. (As commented,
the third will fail with some obscure warning about temporary objects -
but there''s no copying taking place, so there''s no temp object, AFAIK).
Hm.

So I checked something else:

void foobar()
{
SomeObj IamUseless;
}

That object is actually constructed, but destroyed after the function.
Now I am a bit unsure how the C++ compiler can determine EACH TIME
which object gets returned (maybe in the depth of an STL container of
whatever), and which one does NOT, so this has to be destroyed.

Usually I use new() for factories, but I wondered, and I checked. And I
don''t get it :) . So maybe there''s a guru in here who can help me out
with some background information? ;)
Cheers & thanks,
Axel.

推荐答案

Axel Bock写道:
Axel Bock wrote:
你好所有,

我试图了解如果我从函数返回一个类
对象会发生什么。
似乎C ++(MinGW)不会调用复制构造函数如果我这样做的话:

SomeObj foo()
{
SomeObj X;
// ....
返回X;
}

在这种情况下,编译器可以自由地优化冗余副本。

但是每次调用函数时都会创建一个新对象,看起来好像这些对象都可以使用(我创建了一个小测试程序来检查这个:)。
所以基本上我可以进入main:

int main()
{
// ....
SomeObj bar = foo();
SomeObj bar2 = foo();
//对于某些奇怪的然而,接下来会失败的原因是:
SomeObj& bar3 = foo();
Hi all,

I am trying to get my head around what happens if I return a class
object from a function.
It seems C++ (MinGW) does not invoke the copy constructor if I do
something like this:

SomeObj foo()
{
SomeObj X;
// ....
return X;
}
The compiler is free to optimise away the redundant copy in this case.
BUT it does create a new object every time the function is invoked, and
it does seem like these objects are usable (I created a little test
program checking this :).
So basically I can go in main:

int main()
{
// ....
SomeObj bar = foo();
SomeObj bar2 = foo();
// for some strange reason the next thing will fail, though:
SomeObj &bar3 = foo();




你不能将临时对象(函数返回)绑定到非const

引用。试试


const SomeObj& bar3 = foo();


-

Ian Collins。



You can''t bind a temporary object (the function return) to a non-const
reference. Try

const SomeObj &bar3 = foo();

--
Ian Collins.


你好,Ian,


你是对的,这很有用。但是为什么返回值应该是const?

我在整个事件中声明const无处可去,并且在返回值和使用之间没有

复制主要的对象。所以我说
会说函数'的返回值实际上不是临时的

对象,它是const是......很奇怪。我可以修改bar和bar2所有

吧...... :)


基本上我不知道在foo()中创建的对象,因为应该是应该在

函数的范围结束后销毁。


但谢谢,这回答了一个问题,虽然用另一个问题替换了另外一个问题;)


欢呼,

Axel。

Hi Ian,

you''re right, this works. But why should that return value be a const?
I have declared const nowhere in the whole thing, and there''s no
copying between the return value and the used object in main. So I
would say the function''s return value is in fact not a temporary
object, and it being const is ... weird. I can modify bar and bar2 all
right ... :)

Basically I wonder about the object created in foo(), because should be
on the stack, in my opinion, and should be destroyed after the
function''s scope ends.

But thanks, that answers one question, although replacing it with
another ;)

cheers,
Axel.


Alex,

我希望这能回答你的问题。当一个物体超出范围时,它会被销毁。这并不一定意味着内存被删除,但是如果您决定使用该对象,则行为是未定义的。所以

虽然对象可能仍在堆栈中,但是没有保证

它不会被覆盖。


-matt

Alex,
I hope this answers your question. When an object goes out of scope, it
is destroyed. This doesn''t necessarily mean that the memory is erased,
but if you decide to use the object, the behavior is undefined. So
while the object may still be on the stack, there are no guarantees
that it won''t be overwriten.

-matt


这篇关于C ++编译器“返回”行为(大师问题;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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