简体   繁体   English

c ++ push_back,非const复制构造函数

[英]c++ push_back, non const copy constructor

I have a class that i want to push_back into a deque. 我有一个班,我想推回到一个双端队列。 The problem is when i push back i need the original object to be changed thus i need a non const copy ctor. 问题是,当我推回时,我需要更改原始对象,因此我需要一个非const复制ctor。 Now if i implement that my const copy ctor gets called. 现在,如果我实现我的const复制ctor被调用。 If i removed the const ctor i get an compile error about no available ctors. 如果我删除了常量,我得到一个关于没有可用的ctors的编译错误。 How do i implement this in a way that i can modify the original struct when i pass it in? 我如何实现这一点,我可以修改原始结构,当我传入它? i need to modify it bc the class destructs objects when it goes out of scope and i would like to tell it not to do so when there is another instance around. 我需要修改它bc当类超出范围时类破坏对象,我想告诉它不要在有另一个实例时这样做。 I cant use boost since my platform doesnt support it. 我不能使用提升,因为我的平台不支持它。

Your problem is that a fundamental requirement of standard containers is that objects are copy-constructible. 您的问题是标准容器的基本要求是对象是可复制构造的。 That not only means that they have a copy constructor, but that also means that if you copy the object, the copy and the original are the same. 这不仅意味着它们具有复制构造函数,而且这也意味着如果复制对象,则副本和原始文件是相同的。

Your object, however, resembles a move-constructor semantic. 但是,您的对象类似于move-constructor语义。 That is, after a move, the new object owns the resource, and the old object is empty. 也就是说,在移动之后,新对象拥有资源,旧对象为空。 That's not supported by deque as of C++03. 从C ++ 03开始,deque不支持这一点。 That is, by the way, the same reason that forbids putting auto_ptr into a container. 顺便说一下,禁止将auto_ptr放入容器的原因相同。

The next C++ version, called c++0x will support those move semantics by introducing special move constructors. 下一个名为c ++ 0x的C ++版本将通过引入特殊的移动构造函数来支持这些移动语义。 Until then, you will have to use an object that shares ownership when you want to put it into a standard container. 在此之前,当您要将对象放入标准容器时,必须使用共享所有权的对象。 That means if you copy your object, and the original goes out of scope, the owned resource is not freed until all the copies go out of scope. 这意味着如果您复制对象,并且原始文件超出范围,则在所有副本超出范围之前,不会释放拥有的资源。 Consider using boost::shared_ptr for example, or wrap it into your class, if you don't want to program your own class managing that. 例如,考虑使用boost :: shared_ptr,或者将它包装到您的类中,如果您不想编写自己的类来管理它。

如果你没有做什么狡猾的资源(见其他意见),然后让你想改变可变将允许你改变它在一个const函数的成员变量。

Depending on what you are trying to do (more details would be nice), you can either modify the object before/after you call push_back or write a simple wrapper class that takes a pointer to your class and can be inserted into a deque . 根据你要做的事情(更多细节会很好),你可以在调用push_back之前/之后修改对象,或者编写一个简单的包装类,它接受指向类的指针并可以插入到deque This object can then do the appropriate thing to your class on construction/destruction/etc. 然后,此对象可以在构造/销毁/等上对您的类执行适当的操作。

You can't do what you're trying to do. 你不能做你想做的事。 You'll have to use pointers, either plain or smart (but not auto_ptr<>). 你必须使用普通或智能指针(但不是auto_ptr <>)。 Why can't you use Boost smart pointers? 你为什么不能使用Boost智能指针? They're pretty lightweight, and should work in all reasonably standard C++ compilers. 它们非常轻量级,应该适用于所有合理的标准C ++编译器。 You don't have to use all of Boost. 您不必使用所有Boost。

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

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