Chinaunix首页 | 论坛 | 博客
  • 博客访问: 8058430
  • 博文数量: 594
  • 博客积分: 13065
  • 博客等级: 上将
  • 技术积分: 10324
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-26 16:44
个人简介

推荐: blog.csdn.net/aquester https://github.com/eyjian https://www.cnblogs.com/aquester http://blog.chinaunix.net/uid/20682147.html

文章分类

全部博文(594)

分类: C/C++

2018-04-12 20:28:04

// 下列代码输出什么?
#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;
}
阅读(3634) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~