错误3:对于初始化和赋值的模糊概念
2013-04-25 11:53
253 查看
class A { public: A(int a):num(a) { cout<<"Constructor"<<endl; } A(const A& a) { num = a.num; cout <<"Copy Constructor"<<endl; } A& operator=(const A& a) { num = a.num; cout <<"Operator"<<endl; return *this; } ~A() { cout<<"Destructor"<<endl; } private: int num; }; int main() { A a(4); A b=a;//Here is initialization,not using operator "=",but the copy constructor. A c(5); c = a;//This is using assignment operator; A e=4;//Call constructor implicitly }
如果在构造函数上加上explicit的话 A e = 4这样的调用就不会发生了,而A b = a 依然可以使用,因为他是初始化,调用的是拷贝构造函数,不是重载了=或者调用了构造函数。如果在拷贝构造函数上加上explicit的话, A b = a就会编译错误了。
以上结果输出为
相关文章推荐
- 数组的概念,定义格式,取值,赋值以及数组的初始化
- 为什么对于类的const成员,只能使用初始化列表,而不能在构造函数内部进行赋值操作
- 明确c++变量的一些概念:初始化,赋值
- 复习几个C++概念:声明与定义、传值与拷贝构造、初始化和赋值
- 执行失败SSIS 错误代码 DTS_E_PRODUCTLEVELTOLOW。产品级别对于 组件“模糊查找”(9516) 而言不足。
- C C++全局变量初始化 initializer element is not constant 错误 c++中为什么不能对全局变量在函数外赋值
- DES算法中“指定键的大小对于此算法无效。”和“指定的初始化向量(IV)与此算法的块大小不匹配。”错误解决方法。
- 常见错误53: 对于虚基类子对象进行默认初始化----读书笔记《C++ gotchas》--感觉不错
- (转)Java变量初始化赋值null
- 类的初始化和赋值
- 关于Excel数组赋值出错的问题, 800A03EC 错误
- C语言之指针的初始化和赋值
- ,虚拟机又内部错误了,没什么程序好放的就摘点概念吧。。
- Error 25007.初始化合成时发生错误。安装程序无法使用 LoadLibraryShim() 加载合成。
- 一个运行时间的错误。概念上的。
- VS中出现“链接器工具错误,XXX工具模块对于SAFESEH映像是不安全的”的解决方法
- Java编程思想重点阐述(部分容易模糊的概念)
- 数组只能在初始化时整体赋初值。以后再赋值只能逐一改变了。
- Unity 简单使用System.Reflection对ScriptableObject数据进行初始化赋值
- 解决“应用程序正常初始化(0x00000005)失败”错误