问一个关于copy constructor的问题
我定义一个类
class smallint
{
public:
smallint(void);
smallint(int value):val(value)
{
cout<<"constructor"<<endl;
}
smallint(const smallint& small):val(small.val)
{
cout<<"copy constructor"<<endl;
}
smallint& operator=(const smallint& small)
{
val=small.val;
cout<<"assigment"<<endl;
return *this;
}
public:
~smallint(void);
private:
int val;
};
然后定义两个对象
smallint si=10;
为什么对象si没有调用copy constructor啊,c++ primer里说si属于copy initialization,先调用constructor创建一个临时的对象,然后调用copy constructor 将这个临时对象copy给si,但是执行的结果却显示si并没有调用copy constructor
既然这样,如果定义一个string str("string")和string str="string"还有什么区别啊?
可能是我理解有误,还望高手指点迷津