C++中,1.iostream的对象不存在拷贝构造函数,因此在重载<<或者>>操作符的时候,出入的参数必须为ostream的引用,不能为ostream的对象。否则,编译不过。也正是因为这样,重载函数返回值必须为ostream的引用,不能为对象。
值传递ostream,会出现提示,转到ios_base的生命中,看到这样的几句话:
private:
ios_base(const ios_base&);
ios_base&
operator=(const ios_base&);
显然,拷贝构造函数与复制操作符都声明为私有。
因此,重载操作符应该如下:
ostream& operator<<(ostream &out , CA &ca)
{
out<<"Hello"<<" ";
}
C++体会(1) iostream对象不提供拷贝构造函数
最新推荐文章于 2024-05-30 22:54:15 发布