简体   繁体   English

使用包含不可复制数据类型的类的向量

[英]Using vectors with classes that contain non-copyable data types

My issue is that I have a class which contains a std::ifstream, std::ofstream, and std::mutex. 我的问题是我有一个包含std :: ifstream,std :: ofstream和std :: mutex的类。 None of these objects can be directly copied as shown in the example below. 这些对象都不能直接复制,如下例所示。

std::ifstream stream1;
std::ifstream stream2;
stream1 = stream2; //<-Compiler Error!

My problem is not that I want to copy any instances of my class, but that the push_back() function in vectors is trying to call the copy constructor for my class. 我的问题不是我要复制我的类的任何实例,而是vector中的push_back()函数试图为我的类调用复制构造函数。 I have designed an example that replicates my issue and pasted it below. 我设计了一个复制我的问题并将其粘贴在下面的示例。

#include <fstream> //for std::ifstream // std::ofstream
#include <vector> //for std::vector
#include <mutex> //for std::mutex

class MyClass
{
public:
    MyClass(int ID) : ID(ID) { }
    std::ofstream outputstream;
    std::ifstream inputstream;
    std::mutex mymutex;
private:
    int ID;
};

int main()
{
    std::vector<MyClass> MyVector;
    MyVector.push_back(MyClass(1)); //<-- Error C2280 'MyClass::MyClass(const MyClass &)': attempting to reference a deleted function

    return 0;
}

I am having issues trying to figure out how to get around this error. 我在试图弄清楚如何解决这个错误时遇到了问题。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Keep in mind, I will never be directly calling the copy constructor as I have no need in my actual scenario to ever copy an instance of this class, but it seems that push_back is calling it and I have been unsuccessful in my attempts to override it. 请记住,我永远不会直接调用复制构造函数,因为我在实际场景中不需要复制此类的实例,但似乎push_back正在调用它并且我在尝试覆盖它时失败了。

Edit: I think one way to fix it would be to use pointers instead of ifstream,ofstream, and mutex objects but I would prefer to avoid this at all costs. 编辑:我认为解决它的一种方法是使用指针而不是ifstream,ofstream和mutex对象,但我宁愿不惜一切代价避免这种情况。

Since you have access to C++11 features, you could use emplace_back to construct the element in place. 由于您可以访问C ++ 11功能,因此可以使用emplace_back来构建元素。

MyVector.emplace_back(1);

However, there is some problem with std::mutex . 但是, std::mutex存在一些问题。 This member has to be a pointer in order for the above to work. 该成员必须是一个指针才能使上述工作。 Otherwise, you could change all the elements of the class to be types that are copyable (eg, pointers). 否则,您可以将类的所有元素更改为可复制的类型(例如,指针)。

The real problem here is that this class contains a std::mutex member. 这里真正的问题是这个类包含一个std::mutex成员。 Which cannot be copied/moved. 哪个无法复制/移动。 This means that classes that have mutex members don't like to live inside vectors. 这意味着具有互斥成员的类不喜欢居住在向量中。

You need a custom copy and/or move constructors, for your class, and implement the appropriate semantics to copy the streams, and figure out what you want to do with the mutex. 您需要为您的类创建自定义副本和/或移动构造函数,并实现相应的语义来复制流,并找出您要对互斥锁执行的操作。

You might try using a vector of pointers to your objects. 您可以尝试使用指向对象的向量。 It is better to use some kind of smart pointer, rather than a vector of raw pointers. 最好使用某种智能指针,而不是原始指针的向量。

ie std::vector< std::shared_ptr< MyClass >> MyVector; 即std :: vector <std :: shared_ptr <MyClass >> MyVector;

now you have a container of pointers to MyClass objects. 现在你有一个指向MyClass对象的指针的容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM