【问题标题】:how to define vector<ifstream> in c++ class如何在 C++ 类中定义向量<ifstream>
【发布时间】:2013-07-24 09:22:33
【问题描述】:

我需要这样的类中的向量成员:

class A
{
private:
vector<ifstream> _files;
public:
bool addFile(const string& filePath);
};

bool A::addFile(const string& filePath)
{
ifstream ifile(filePath.c_str());
_files.push_back(ifile);//but errors;
}

我怎样才能成功编译完成这门课;

现在我的解决方案是使用矢量。那样可以么?还是一些潜在的危险?

【问题讨论】:

    标签: c++ vector fstream


    【解决方案1】:

    STL 容器需要元素是 CopyConstructibleAssignablestd::ifstream 不可复制。您需要使用指向std::ifstream 的智能指针。

    【讨论】:

    • 如果您有权访问std::unique_ptr,您已经可以将它们存储在向量中。
    • 不会有危险。你最好使用智能指针来存储它。
    猜你喜欢
    • 2021-12-29
    • 1970-01-01
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多