的问题Constructor调用

的问题Constructor调用

本文介绍了关于C ++的问题Constructor调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚这两行代码之间的区别。



我们给出的Count是我们的类名,C1和C2该类的对象。

 计数C2(C1); //语句1 
计数C2 = C1; //语句2

没有提供其他信息。这两行调用的拷贝构造函数有什么区别?

解决方案

$ b 在语法层面,第一个称为直接初始化,第二个称为复制初始化。如果 Count 是类类型(即不是 int 的typedef),则两个版本



第一个版本在任何情况下都可以工作,如果复制构造函数声明为 explicit


I'm trying to figure out the difference between these two lines of code..

We are given 'Count' is our Class name, C1 and C2 are objects of that class. No information of how and when the classes have been declared are given.

Count C2(C1); //Statement 1
Count C2=C1;  //Statement 2

No other information is given. What is the difference between these two lines of call for copy constructor? Please elaborate if you have the answer.

Thanks!

解决方案

At the grammatical level, the first one is called "direct initialization" and the second one is called "copy initalization". If Count is of class type (i.e. not a typedef of int, say), then both versions, equivalently, cause the copy constructor to be called.

The first version works in any case, the second version does not work if the copy constructor is declared explicit.

这篇关于关于C ++的问题Constructor调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 18:07