【问题标题】:Remove code duplication with an ostream variable使用 ostream 变量删除重复代码
【发布时间】:2020-05-15 21:04:04
【问题描述】:

我有

void fnc(std::ofstream& file){
    std::cout << x;
    file << x;
}

x 有点复杂,我想删除代码重复。

我尝试了类似的东西

void fnc(std::ofstream& file){
    std::ostream os;
    os << x;
    std::cout << os;
    file << os;
}

但它不起作用。使用运算符 删除代码重复的最佳方法是什么

【问题讨论】:

标签: c++ ofstream ostream


【解决方案1】:

感谢drescherjm,解决方案如下:

void fnc(std::ofstream& file){
    std::ostringstream os;
    os << x;
    std::cout << os.str();
    file << os.str();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    相关资源
    最近更新 更多