在“=”中调用复制构造函数操作者 [英] call copy constructor in "=" operator

查看:93
本文介绍了在“=”中调用复制构造函数操作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,


我是c +的新手,当写下代码试图打电话给副本

构造函数

in=运算符重载,它无法编译。任何人都可以指出



我的原因是什么?谢谢!!


等级AA

{

公开:


AA( AA& obj)

{

}


AA& operator =(AA& obj)

{

AA(obj);

返回* this;

}


};


int main()

{


返回0;

}


pp.cc:方法`AA级& AA :: operator =(AA&)'':

pp.cc:11:'obj''声明阴影参数

pp.cc:11:没有匹配函数来调用`AA :: AA()''

pp.cc:6:候选人是:AA :: AA(AA&)

解决方案

* fo ********* @ gmail.com


我是c ++的新手,当编写下面的代码试图在" ="中调用copy
构造函数时运算符重载,它无法编译。可以
有人指出我的原因吗?谢谢!!

AA级


风格:不要使用除宏之外的所有大写名称。


{
公开:

AA(AA& obj)
{
* fo ********* @ gmail.com :}


除非有充分理由做其他事情,复制构造函数的参数应该是对const的引用,


AA(AA const& other){}


AA& operator =(AA& obj)
{
AA(obj);


这将声明一个类型AA的对象obj,_if_类型AA有一个默认的

构造函数和_if_你没有名称冲突:


AA(obj);


表示


AA obj;


并导致编译器发出

pp.cc:在方法`类AA& AA :: operator =(AA&)'':
pp.cc:11:'obj''的声明为名称冲突隐藏参数




pp.cc:11:没有匹配函数来调用`AA :: AA()''
pp.cc:6:候选人是:AA :: AA(AA& )


缺少默认构造函数。


可能你想要的是


AA副本(obj);


返回* this;




此时此刻回归还为时过早;你仍然需要更新分配给的

对象!

你所拥有的是作业来源的副本,以及
$的成语b $ b以这种方式使用复制构造函数依赖于交换该副本的

内容和分配给的对象,


swap(copy);


其中swap是一个交换内容的成员函数,并且

保证不会抛出。


然后你可以返回,作为交换的一部分,析构函数是b $ b负责破坏例如早先动态分配内存

由*持有但现在存在于副本中。


-

答:因为它搞砸了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门帖子。

Q :usenet和电子邮件中最烦人的事情是什么?


fo ********* @ gmail.com 写道:

亲爱的所有人,

我是c +的新手,写的时候下面的代码试图调用复制
构造函数
在" ="运算符重载,它无法编译。谁能指出
我的原因?谢谢!!

AA级
{
公开:

AA(AA& obj)

}

AA& operator =(AA& obj)
{
AA(obj);
返回* this;
}

};

int main()


返回0;
}

pp.cc:方法`AA类& AA :: operator =(AA&)'':
pp.cc:11:声明`obj''阴影参数
pp.cc:11:没有匹配函数来调用AA :: AA()''
pp.cc:6:候选人是:AA :: AA(AA&)



这通常不是一个好主意,因为构造函数

不应该清理当前正在使用的任何动态分配的

内存,而operator =()应该是

。因此,你的代码可能会产生内存泄漏和
GP错误!


JB


fo ********* @ gmail.com 写道:

亲爱的全部,

我是c +的新手,当写下代码试图调用复制
构造函数




你不能调用构造函数。当你创建一个对象时,就会为你调用它们。* * *只有当你创建一个对象时。当你执行

赋值时,你不会创建任何东西。也许这有助于

理解为什么你不能调用复制构造函数。


显然你正试图减少重复的代码。在这种情况下,它通常是一个好主意,创建一些私人帮助函数来完成工作。我的偏好:破坏,创造和复制。你的析构函数调用

destroy,默认构造函数调用create,复制构造函数调用copy

,赋值运算符首先调用destroy然后复制等。


hth

-

jb


(rot13的回复地址,先解读)


Dear All,

I am new to c++ and when write below code that try to call copy
constructor
in "=" operator overloading, it can not compile. Can anyone point out
for
me the reason? thanks !!

class AA
{
public:

AA( AA & obj)
{
}

AA & operator=( AA & obj)
{
AA(obj);
return *this;
}

};

int main()
{

return 0;
}

pp.cc: In method `class AA & AA::operator =(AA &)'':
pp.cc:11: declaration of `obj'' shadows a parameter
pp.cc:11: no matching function for call to `AA::AA ()''
pp.cc:6: candidates are: AA::AA(AA &)

解决方案

* fo*********@gmail.com:


I am new to c++ and when write below code that try to call copy
constructor in "=" operator overloading, it can not compile. Can
anyone point out for me the reason? thanks !!

class AA
Style: don''t use all uppercase names except for macros.

{
public:

AA( AA & obj)
{ * fo*********@gmail.com: }
Unless there is a very good reason to do something else, the argument to
the copy constructor should be a reference to const,

AA( AA const& other ) {}

AA & operator=( AA & obj)
{
AA(obj);
This would declare an object obj of type AA, _if_ type AA had a default
constructor and _if_ you didn''t have a name conflict:

AA (obj);

means

AA obj;

and causes the compiler to issue
pp.cc: In method `class AA & AA::operator =(AA &)'':
pp.cc:11: declaration of `obj'' shadows a parameter
for the name conflict, and
pp.cc:11: no matching function for call to `AA::AA ()''
pp.cc:6: candidates are: AA::AA(AA &)
for the lack of a default constructor.

Probably what you intended was

AA copy( obj );

return *this;



At this point it''s too early to return; you still have to update the
object assigned to!

What you have is a copy of the assignment source, and the idiom for
using the copy constructor in this way relies on then swapping the
contents of that copy and the object assigned to,

swap( copy );

where swap is a member function that swaps the contents and is
guaranteed to not throw.

Then you can return, and as part of that the swap the destructor is
responsible for destroying e.g. dynamically allocated memory earlier
held by *this but now residing in copy.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


fo*********@gmail.com wrote:

Dear All,

I am new to c++ and when write below code that try to call copy
constructor
in "=" operator overloading, it can not compile. Can anyone point out
for
me the reason? thanks !!

class AA
{
public:

AA( AA & obj)
{
}

AA & operator=( AA & obj)
{
AA(obj);
return *this;
}

};

int main()
{

return 0;
}

pp.cc: In method `class AA & AA::operator =(AA &)'':
pp.cc:11: declaration of `obj'' shadows a parameter
pp.cc:11: no matching function for call to `AA::AA ()''
pp.cc:6: candidates are: AA::AA(AA &)


That is not generally a good idea because a constructor
should not have to clean up any dynamically allocated
memory currently in use whereas the operator =() should
do. Hence your code could develop memory leaks and
GP faults!!

JB


fo*********@gmail.com wrote:

Dear All,

I am new to c++ and when write below code that try to call copy
constructor



You cannot call constructors. They are called for you when you
create an object .. and *only* when you create an object. When you do
assignment, you do not create anything. Maybe this helps in
understanding why you cannot call the copy constructor.

Obviously you are trying to reduce duplicate code. In this case, it
is usually a good idea to create little private helper functions that do
the job. My preference: destroy, create and copy. Your destructor calls
destroy, default constructor calls create, copy constructor calls copy
and the assignment operator first calls destroy and then copy etc.

hth
--
jb

(reply address in rot13, unscramble first)


这篇关于在“=”中调用复制构造函数操作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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