关于拷贝构造的一些问题

圣诞老人123 2011-08-01 04:02:14
3. Why the following A(A a) is illegal.
class A
{
public:
A(A a); // error
A(A a, int i); // allowed
};

谁能帮我解释下啊

我想可能是使用了下面这个
A(A a); // error
然后编译器就不会生成默认拷贝构造函数了

A b;
A a(b);
时候由于A(A a) 其中A 不是reference的话 就还需要调用拷贝构造 此时编译器没有帮助生成拷贝构造 所以 不对~~
不知道我想的对不对~
...全文
96 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Louistao 2011-08-01
  • 打赏
  • 举报
回复
标准这东西还是别去读了。。
luciferisnotsatan 2011-08-01
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 chenyongsuda 的回复:]

恩~~~大致了解了,

那问下

A(const A& a)
{
qDebug() << "const copy construct01";
}
A(A& a)
{
qDebug() << "copy construct01";
}

调用A b;
A a(b,1);
……
[/Quote]
下个C++ 2003标准,CSDN下载里就有
luciferisnotsatan 2011-08-01
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 pengzhixi 的回复:]

标准规定了你那种形式就是错误的:

A declaration of a constructor for a class X is ill-formed if its first parameter is of type (optionally cvqualified)X and either there are no other parameters or else all other p……
[/Quote]
++

A(A a);
拷贝构造函数是传值,会调用A(A a)拷贝构造函数,产生个副本,无限递归了。
懒得打字 2011-08-01
  • 打赏
  • 举报
回复
同想知道怎么查到的
pengzhixi 2011-08-01
  • 打赏
  • 举报
回复
选择调用哪一个属于重载解析的问题了。这里面有一个最佳匹配原则。
圣诞老人123 2011-08-01
  • 打赏
  • 举报
回复
恩~~~大致了解了,

那问下

A(const A& a)
{
qDebug() << "const copy construct01";
}
A(A& a)
{
qDebug() << "copy construct01";
}

调用A b;
A a(b,1);
A a(b);

会输出copy construct01 原因是什么?


pengzhixi 大哥~~你这个是在什么书上查的~~~~~
能不能教教我怎么去找这些方面的答案的~~
至善者善之敌 2011-08-01
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 pengzhixi 的回复:]
标准规定了你那种形式就是错误的:

A declaration of a constructor for a class X is ill-formed if its first parameter is of type (optionally cvqualified)X and either there are no other parameters or else all other pa……
[/Quote]

++1
pengzhixi 2011-08-01
  • 打赏
  • 举报
回复
标准规定了你那种形式就是错误的:

A declaration of a constructor for a class X is ill-formed if its first parameter is of type (optionally cvqualified)X and either there are no other parameters or else all other parameters have default arguments
Michael_Xie 2011-08-01
  • 打赏
  • 举报
回复
c++规定的
healer_kx 2011-08-01
  • 打赏
  • 举报
回复
拷贝构造函数的参数是需要 const&修饰的。你那个不是copy ctor。

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧