How do I use the ostringstream properly in c++?

那年仲夏 提交于 2019-12-03 23:30:17
André Oriani

Declare int_buffer, float_buffer, and float_buffer2 inside toString() function. Because you are declaring in the class, those objects are kept around, so every time you call toString() function you are concatenating to int_buffer, float_buffer, and float_buffer2 over and over. If you declare inside the method they will exist only while the toString is active. Anyway, you are doing too much code for what you are trying to do. You could simply do:

std::string toString()
{
    std::ostringstream buffer; 
    buffer << "Product name: "<< description << " Units left: " << on_hand << " Price: "<< price << " Revenue: $" << generated_revenue;
    return buffer.str();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!