为什么抛出的对象被复制了两次? [英] why are thrown objects copied twice?

查看:58
本文介绍了为什么抛出的对象被复制了两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想知道为什么要抓到的对象是多次复制构造的




我在运行以下简单程序之后看到了这种行为

这是一个练习(改编自F.Brokken C ++ Annotations)仅仅是为了
看工作中的施工人员:


/ * exception.cpp * /


#include< iostream>

#include< string>


使用命名空间std;


class Object

{

字符串名称;

公开:

对象(字符串n)

:名称(n)

{

cout<< 对象构造函数 <<名称<< " \ n";

}

对象(对象const& other)

:name(other.name +"(复制))

{

cout<< 复制构造函数 <<名称<< " \ n";

}

~Object()

{

cout<< 对象析构函数 <<名称<< " \ n";

}

friend void fun_1(对象obj)

{}

无效fun_2()

{

Object Thrown(" local object");

throw thrrow;

}

char const * get()

{

返回name.c_str();

}

};


int main()

{

Object out(" main object");


fun_1(out);


试试

{

out.fun_2 ();

}

catch(对象obj)

{

cout<< 被抓住的例外 << obj.get()<< " \ n";

}

}


这是输出:


[我自己@ localhost / temporary]#。/ exception

主对象的对象构造函数

复制主对象的构造函数(copy)

主对象的对象析构函数(副本)

本地对象的对象构造函数

复制本地对象的构造函数(副本)

对象析构函数本地对象

复制本地对象的构造函数(复制)(复制)

从本地对象捕获异常(复制)(复制)

对象析构函数本地对象(副本)(副本)

本地对象的对象析构函数(副本)

主对象的对象析构函数


如您所见,复制构造函数添加了(复制)。到

新创建的对象的名称。我也打电话给fun_1()只是为了说服

我自己认为通过值传递的对象只复制一次。


可能是我遗漏了一些明显的东西。


提前谢谢大家,

Fabio De Francesco。

Hi,

I would like to know why objects that are thrown to be caught are
copy-constructed more than once.

I have seen this behaviour after running the following simple program
that is an exercise (adapted from F.Brokken C++ Annotations) only to
see constructors at work:

/* exception.cpp */

#include <iostream>
#include <string>

using namespace std;

class Object
{
string name;
public:
Object(string n)
:name(n)
{
cout << "Object constructor of " << name << "\n";
}
Object(Object const &other)
:name(other.name + " (copy)")
{
cout << "Copy constructor for " << name << "\n";
}
~Object()
{
cout << "Object destructor of " << name << "\n";
}
friend void fun_1(Object obj)
{}
void fun_2()
{
Object Thrown("local object");
throw Thrown;
}
char const *get()
{
return name.c_str();
}
};

int main()
{
Object out("main object");

fun_1(out);

try
{
out.fun_2();
}
catch(Object obj)
{
cout << "Caught exception from " << obj.get() << "\n";
}
}

This is the output:

[myself@localhost /temporary]# ./exception
Object constructor of main object
Copy constructor for main object (copy)
Object destructor of main object (copy)
Object constructor of local object
Copy constructor for local object (copy)
Object destructor of local object
Copy constructor for local object (copy) (copy)
Caught exception from local object (copy) (copy)
Object destructor of local object (copy) (copy)
Object destructor of local object (copy)
Object destructor of main object

As you can see the copy constructor adds "(copy)" to the name of the
newly created object. I also put the call to fun_1() just to persuade
myself that an object that is passed by value is copied only once.

May be I am missing something obvious.

Thank you all in advance,

Fabio De Francesco.

推荐答案

fm**@tiscali.it (fabio de francesco)写在

新闻:ba ************************** @ posting.google.c om:
fm**@tiscali.it (fabio de francesco) wrote in
news:ba**************************@posting.google.c om:


我想知道为什么被抛出的物体不止一次被复制构造。
Hi,

I would like to know why objects that are thrown to be caught are
copy-constructed more than once.




您正在追逐价值而非参考。这将导致一个

副本由抛出的对象组成,就像你将它按照值传递给函数一样。

而不是


catch(对象obj)


尝试


catch (const Object& obj)


第一个副本是在你投掷非临时的本地

对象时制作的。


Gregg



You are catching by value instead of by reference. This will result in a
copy being made of the thrown object, just as it would if you passed it
to a function by value.

Instead of

catch (Object obj)

try

catch (const Object& obj)

The first copy is made when you are throwing the non-temporary local
object.

Gregg


Fabio De Francesco写道:
Fabio De Francesco wrote:
我想知道为什么抛出的物体是
复制构造不止一次。


格雷格回答说:
你正在追逐价值而非参考。这将导致复制被抛出的对象,就像你通过值传递给一个函数一样。

而不是

catch(对象obj)

尝试

catch(const Object& obj)
I would like to know why objects that are thrown to be caught are
copy-constructed more than once.
Gregg replied:
You are catching by value instead of by reference. This will result in a
copy being made of the thrown object, just as it would if you passed it
to a function by value.

Instead of

catch (Object obj)

try

catch (const Object& obj)




Doesn'在法比奥的情况下工作,因为他想要调用obj的非const

成员函数。在catch子句中。相反,使用非const引用的是什么?b $ b?如下:


catch(Object& obj)

问候,


Niels Dekker
< a rel =nofollowhref =http://www.xs4all.nl/~nd/dekkerwaretarget =_ blank> www.xs4all.nl/~nd/dekkerware




" Niels Dekker - 没有回复地址 <未***** @ this.is.invalid>在消息中写道

news:41 ************** @ this.is.invalid ...

"Niels Dekker - no reply address" <un*****@this.is.invalid> wrote in message
news:41**************@this.is.invalid...
Fabio De Francesco写道:
Fabio De Francesco wrote:
我想知道为什么被抛出的对象被多次复制构造。
I would like to know why objects that are thrown to be caught are
copy-constructed more than once.



Gregg回复:



Gregg replied:


您正在追逐价值而非参考。这将导致复制被抛出的对象,就像你通过值传递给一个函数一样。

而不是

catch(对象obj)

尝试

catch(const Object& obj)

You are catching by value instead of by reference. This will result in a
copy being made of the thrown object, just as it would if you passed it
to a function by value.

Instead of

catch (Object obj)

try

catch (const Object& obj)



不能工作法比奥的情况,因为他想要调用obj的非const
成员函数。在catch子句中。相反,使用非const引用的内容是什么?如下:

catch(Object& obj)



Doesn''t work in Fabio''s case, because he wants to call a non-const
member function of "obj" inside the catch clause. So instead, what
about using a non-const reference? As follows:

catch (Object& obj)




它没关系,但更好的是将get声明为const,就像它本来应该是




char const * get()const

{

返回name.c_str();

}


john



It''s OK, but better would be to declare get as const, like it should''ve been
in the first place.

char const *get() const
{
return name.c_str();
}

john


这篇关于为什么抛出的对象被复制了两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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