关于拷贝构造函数的一个选择题 最好能给出解释

林初茵 2008-11-24 12:42:54
When a Copy Constructor is not written for a class, the C++ compiler generates one. Which of the following statements correctly describe the actions of this compiler-generated Copy Constructor when invoked? (当类中没有拷贝构造函数时,编译器会自己生成1个,下列说法正确的是:)
A. The compiler-generated Copy Constructor makes the object being constructed, a reference to the object passed to it as an argument. (编译器生成的拷贝构造函数使对象被构造,对象的引用作为一个参数)
B. The compiler-generated Copy Constructor does not do anything by default. (编译器生成的拷贝构造函数默认情况什么都不做)
C. The compiler-generated Copy Constructor performs a member-wise copy of the object passed to it as an argument, into the object being constructed. (编译器生成的拷贝构造函数执行,一个对象的成员拷贝作为一个参数,传入对象被构造)
D. The compiler-generated Copy Constructor tags the object as having been Copy-Constructed by the compiler. (编译器生成的拷贝构造函数给对象贴标签作为被“拷贝构造”了)
E. The compiler-generated Copy Constructor invokes the assignment operator of the class. (编译器生成的拷贝构造函数调用类的运算符)
...全文
272 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
lq651659889 2008-11-24
  • 打赏
  • 举报
回复
B
林初茵 2008-11-24
  • 打赏
  • 举报
回复
各位牛人 到底选哪个? 为什么
rejoice914 2008-11-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ztz0223 的回复:]

复制错了
[/Quote]
哈哈
eatsweetpotato 2008-11-24
  • 打赏
  • 举报
回复
sorry ,答非所问。
就呆在云上 2008-11-24
  • 打赏
  • 举报
回复

复制错了
eatsweetpotato 2008-11-24
  • 打赏
  • 举报
回复
举个最简单的例子,假如类里面有指针,那么就应该自己定义一个复制构造函数,因为默认的复制构造函数是浅拷贝。
rejoice914 2008-11-24
  • 打赏
  • 举报
回复
A是深度拷贝
C是浅度拷贝
就呆在云上 2008-11-24
  • 打赏
  • 举报
回复
B. The compiler-generated Copy Constructor does not do anything by default. (编译器生成的拷贝构造函数默认情况什么都不做) 
rejoice914 2008-11-24
  • 打赏
  • 举报
回复
c吧
rainbow_free 2008-11-24
  • 打赏
  • 举报
回复
mark
yndfcd 2008-11-24
  • 打赏
  • 举报
回复
A C正确.

B什么也不做当然是错的.

D纯粹是在胡扯.

E拷贝构造函数不会调用该类的赋值运算符.
overbai 2008-11-24
  • 打赏
  • 举报
回复
A compiler-generated default constructor sets up the object (initializes vftables and vbtables, as described previously), and it calls the default constructors for base classes and members, but it takes no other action.

A compiler-generated copy constructor sets up a new object and performs a memberwise copy of the contents of the object to be copied. If base class or member constructors exist, they are called; otherwise, bitwise copying is performed.


If all base and member classes of a class type have copy constructors that accept a const argument, the compiler-generated copy constructor accepts a single argument of type const type&. Otherwise, the compiler-generated copy constructor accepts a single argument of type type&.
overbai 2008-11-24
  • 打赏
  • 举报
回复
A C E正确
林初茵 2008-11-24
  • 打赏
  • 举报
回复
如果在类中根本没有构造函数,编译
器将自动地创建一个缺省的构造函数。不过在这种情况下,这个构造函数什么也不做。然而,
如果我们加了一个拷贝构造函数,我们就告诉了编译器我们将自己处理构造函数的创建,编译
器将不再为我们创建缺省的构造函数。并且除非我们显式地创建一个缺省的构造函数,否则,编译器会指示出错。
林初茵 2008-11-24
  • 打赏
  • 举报
回复
因为拷贝构造函数实现传值方式的参数传递和返回,所以在这种简单结构情况下,编译器
将有效地创建一个缺省拷贝构造函数,这非常重要。在C中也是这样。然而,直到目前所看到
的一切都是缺省的原始行为:位拷贝( b i t c o p y)。
当包括更复杂的类型时,如果没有创建拷贝构造函数, C + +编译器也将自动地为我们创建
拷贝构造函数。这是因为在这里用位拷贝是没有意义的,它并不能达到我们的目的。
henry_dx 2008-11-24
  • 打赏
  • 举报
回复
up
jiyuanjie 2008-11-24
  • 打赏
  • 举报
回复
应该是A,C吧,
B肯定是不对的,默认拷贝构造函数会缺省的按成员初始化
D这个做法没有听说过
E不对,不管是默认的还是自定义的赋值构造和复制赋值=都应该是两回事

65,211

社区成员

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

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