zoukankan      html  css  js  c++  java
  • C++的重载流输出运算符

    // 下列代码输出什么?
    #include <iostream>
    #include <string>

    // typedef basic_ostream<char> 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<typename _CharT, typename _Traits, typename _Alloc>
    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 <iostream>
    #include <string>

    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;
    }
  • 相关阅读:
    ASP.NET中Cookie编程的基础知识
    一道编程题
    软件开发一点心得
    迅雷产品经理笔试题
    常用JS 1
    设计模式
    整理思路
    抽象工厂模式 Abstract Factory
    单件模式(Single Pattern)
    序列化
  • 原文地址:https://www.cnblogs.com/aquester/p/9891486.html
Copyright © 2011-2022 走看看