问答

c++ 怎样重载<<操作符才能连续使用?

作者:admin 2021-04-21 我要评论

请教,如下代码怎样才能连续使用 1 2 ? 还有 CR ; #include iostreamclass myOutText{public: int operator (auto s){ std::cout s; } void CR(){ putchar('\n')...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

请教,如下代码怎样才能连续使用 << 1 << 2 ?
还有 << CR ;

#include <iostream>

class myOutText{
public:
    int &operator << (auto s){
        std::cout << s;
    }

    void CR(){
        putchar('\n');
    }

};

int main(void)
{
    myOutText cout; 
    cout << "string<<";
    cout.CR();            // 怎样实现 cout << CR ; 
    cout << "1<<2<<\n";
    cout << 1 << 2;        // 怎样才能连续输出 12 
        
    return 0;
}
###
#include <iostream>

class myOutText{
public:
    myOutText &operator << (auto s) {   // 返回自身引用以支持连续操作
        std::cout << s;
        return *this;
    }

    void CR() {
        putchar('\n');
    }
};

int main(void)
{
    myOutText cout; 

    cout << "string<<";
    cout.CR(); 
    cout << "1<<2<<\n";
    cout << 1 << 2 << '\n';
        
    return 0;
}

输出:

book@100ask:~/Desktop$ g++ test.cpp 
book@100ask:~/Desktop$ ./a.out 
string<<
1<<2<<
12
###

谢谢指点,还有第二个问题也麻烦解决一下

怎样实现 cout << CR ; //相当于 std::cout << std::endl;

把回车输出给 myOutText cout ?


自己想了一个变通的办法:

#include <iostream>
#include <string>
#define CR "\n"

class myOutText{
public:
    myOutText &operator << (std::string s) {
        if (s==CR) std::cout << CR;
        else std::cout << "[string:" << s << "]";
        return *this;
    }

    myOutText &operator << (long double s) {
        std::cout << "[number:" << s << "]";
        return *this;
    }

};

int main(void)
{
    myOutText cout; 

    cout << "string1-" << "abc";
    cout << CR;
    cout << "1<<2<<:" << CR;
    cout << 1 << 23 << 1.2e-5 << 2.5 << CR << "xyz" << CR;
    cout << "end" << CR;
        
    return 0;
}

输出结果:

[string:string1-][string:abc]
[string:1<<2<<:]
[number:1][number:23][number:1.2e-005][number:2.5]
[string:xyz]
[string:end]

--------------------------------
Process exited after 0.01708 seconds with return value 0
请按任意键继续. . .

【新问题】
改进代码后,不用return *this; 也能连续使用,这又是为什么呢?

#include <iostream>
#include <string>
#include <typeinfo>
#define endl "\n"

class myOutText{
public:
    myOutText &operator << (auto s) {
        std::string str;
        if (typeid(s).name()==typeid(std::string("")).name()) str=s;
        if (str==endl) { 
            std::cout << endl;
            std::cout.flush();
        }
        else
            std::cout << s ;

        //return *this; //返回自身引用以支持连续操作
    }
};

int main(void)
{
    myOutText cout; 

    auto a = "abc";
    double b = 8.9;
    cout << "string1-" << a;
    cout << endl;
    cout << "1<<23<<4.56:" << endl;
    cout << 1 << 23 << 4.56e-7 << " "
         << b << endl << "crlf" << endl;
         
    std::string str = "endl";
    cout << str << endl << endl;
            
    return 0;
}

输出结果:

/*
string1-abc
1<<23<<4.56:
1234.56e-007 8.9
crlf
endl


--------------------------------
Process exited after 1.198 seconds with return value 0
请按任意键继续. . .
*/

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • c++ 怎样重载&lt;&lt;操作符才

    c++ 怎样重载&lt;&lt;操作符才

  • react this

    react this

  • 请问forge加载模型的周期是怎么样的?

    请问forge加载模型的周期是怎么样的?

  • thinkphp5.0安装phpword扩展失败,怎么

    thinkphp5.0安装phpword扩展失败,怎么

腾讯云代理商
海外云服务器