运算符重载 - 为什么编译器C++不为类提供重载插入器和提取器函数?
operator overloading - Why C++ compiler doesn't provide overloaded inserter & extractor functions for class?
考虑以下程序。
#include <iostream>
using std::ostream;
using std::cout;
using std::istream;
using std::cin;
class three_d {
int i,j,k;
public:
three_d(int a,int b,int c) : i(a),j(b),k(c)
{ }
//friend ostream& operator << (ostream&,const three_d&);
//friend istream& operator >> (istream&,three_d&);
};
/*ostream& operator << (ostream& o,const three_d& t) {
o<<t.i<<", ";
o<<t.j<<", ";
o<<t.k<<"n";
return o;
}
istream& operator >> (istream& stream,three_d &t) {
cout<<"Enter x,y,z values";
stream>>t.i>>t.j>>t.k;
return stream;
}*/
int main() {
three_d a(1,2,3),b(4,5,6),c(7,8,9);
cout<<a<<b<<c;
cin>>a;
cout<<a;
}
我故意注释掉了重载的>> && lt; & lt;操作符函数定义和编译器给出以下错误消息
*[Error] no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'three_d')
[Error] no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' and 'three_d')*
默认情况下,即使是空类,如果程序员没有提供,编译器也会自动提供以下内容。
1)构造函数2)析构函数3)复制构造函数
4) =(赋值)操作符
那为什么呢>> && lt; & lt;操作符必须由程序员显式重载才能使用用户定义的类型(类)?如果编译器默认提供重载>> && lt; & lt;用户定义类型的操作符函数也?这难道不是减少了编写&减轻程序员的负担?
那为什么呢>> && lt; & lt;操作符必须由程序员显式重载才能使用用户定义的类型(类)?
您列出的默认值都不提供序列化或反序列化,这是两个非常复杂的问题,通常难以解决。
您列出的所有四种东西都允许在语义上重新排序它们的操作,但是序列化/反序列化不允许。
另外,序列化的数据应该采用什么格式?二进制还是文本?数据字段之间应该用什么分隔符?数据字段应该命名还是简单排序?
最后,最初的四个都不会失败,然而istream
有很多原因。失败了怎么办?是返回部分初始化和部分默认的类,还是抛出异常?
此外,到目前为止,反对这个想法的最大因素是添加功能的固有成本。这是关于这个话题的答案(有一些关于这个话题的更全面的博客文章,但我没有现成的链接,抱歉)
相关文章:
- 重载运算符的范围是什么?它是否会影响作为类成员的集合的插入函数?
- 我的运算符重载是否有效<<(流插入)左操作数不是 ostream
- 重载嵌套结构/类的流插入运算符
- 在设计方面:重载vector类型的类成员的插入运算符
- 类重载运算符 '<' 插入指向该对象集的共享指针时不调用
- 基类和派生类的虚拟插入运算符重载
- 如何在 std::map<const int、int> C++ 中重载插入运算符>>?
- 派生类 插入和提取运算符重载以及基类与派生类之间的强制转换
- 带有指针的重载插入/提取运算符
- C++序列化:使用write()重载插入运算符
- 重载插入运算符(运算符<<)
- 在 Boost.Log (Boost 1.60.0) 中重载插入运算符
- 重载插入操作
- 类的重载C++插入运算符 (<<)
- 我需要在这里重载插入运算符吗
- >>运算符重载插入?
- 为自己的流类重载插入操作符
- 重载插入操作符:没有找到接受右操作数'unsigned int'(或者没有可接受的转换)
- 是否可以重载插入运算符>>在不使用数组的情况下接收用户输入?
- 运算符重载 - 为什么编译器C++不为类提供重载插入器和提取器函数?