求解惑,关于初始化!
看书上说初始化分两种:直接初始化和复制初始化int ival(1024);
int ival=1024;
这两种初始化有区别吗?如果有区别在哪?
各路大神,求解惑,谢谢哦
#include <iostream> using namespace std; struct foo { foo( int val ) : val_(val) { cout << "foo(int)" << endl; } explicit foo( float f ) : val_(int(f)) { cout << "foo(float)" << endl; } //private: foo( const foo& obj ) : val_(obj.val_) { cout << "foo(foo)" << endl; } foo& operator=( const foo& obj ) { cout << "=" << endl; return *this; } private: int val_; }; int main(void) { foo a = 0; cout << "----------------" << endl; foo b = 0.0f; cout << "----------------" << endl; foo c = foo(0.0f); cout << "----------------" << endl; foo d = foo(0); cout << "----------------" << endl; return 0; }体会一下,为什么是这个输出结果