首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

C++的重载流输出运算符

// 下列代码输出什么? #include #include // typedef basic_ostream ostream; class A { private:     int m1,m2; public:     A(int a, int b) {         m1=a;m2=b;     }     operator std::string() const { return "str"; }     operator int() const { return 2018; } }; int main() {     A a(1,2);     std::cout << a;     return 0; }; 答案是2018, 因为类basic_ostream有成员函数operator<<(int), 而没有成员函数operator<<(const std::string&), 优先调用同名的成员函数,故输出2018,相关源代码如下: // 名字空间std中的全局函数 /usr/include/c++/4.8.2/bits/basic_string.h: template inline basic_ostream<_CharT, _Traits>& operator <<(basic_ostream<_CharT, _Traits>& __os,             const basic_string<_CharT, _Traits, _Alloc>& __str) {     return __ostream_insert(__os, __str.data(), __str.size()); } // 类basic_ostream的成员函数 //  std::cout为名字空间std中的类basic_ostream的一个实例 ostream: __ostream_type& basic_ostream::operator<<(int __n); // 下列代码有什么问题,如何修改? #include #include class A { public:     int m1,m2; public:     A(int a, int b) {         m1=a;m2=b;     }     std::ostream& operator <<(std::ostream& os) {         os << m1 << m2; return os;     } }; int main() {     A a(1,2);     std::cout << a;     return 0; }; 类basic_ostream没有成员函数“operator <<(const A&)”, 也不存在全局的: operator <<(const basic_ostream&, const A&) 而只有左操作数是自己时才会调用成员重载操作符, 都不符合,所以语法错误。 有两种修改方式: 1) 将“std::cout << a”改成“a.operator <<(std::cout)”, 2) 或定义全局的: std::ostream& operator<<(std::ostream& os, const A& a) {     os << a.m1 << a.m2; return os; }

04
您找到你想要的搜索结果了吗?
是的
没有找到
领券