c++ Push_back,非常量复制构造函数
我有一个类,我想将其推回到双端队列中。 问题是,当我推迟时,我需要更改原始对象,因此我需要一个非常量复制因子。 现在,如果我实现我的 const copy ctor 被调用。 如果我删除了 const ctor,我会收到有关没有可用 ctor 的编译错误。 我如何以一种在传递原始结构时可以修改原始结构的方式来实现它? 我需要修改它,因为该类在超出范围时会破坏对象,并且我想告诉它在存在另一个实例时不要这样做。 我无法使用 boost,因为我的平台不支持它。
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. Now if i implement that my const copy ctor gets called. If i removed the const ctor i get an compile error about no available 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. I cant use boost since my platform doesnt support it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的问题是标准容器的基本要求是对象是可复制构造的。 这不仅意味着它们有一个复制构造函数,而且还意味着如果复制该对象,则副本和原始对象是相同的。
然而,您的对象类似于移动构造函数语义。 也就是说,移动后,新对象拥有资源,而旧对象为空。 从 C++03 开始,双端队列不支持这一点。 顺便说一句,这与禁止将 auto_ptr 放入容器的原因相同。
下一个 C++ 版本(称为 c++0x)将通过引入特殊的移动构造函数来支持这些移动语义。 在那之前,当您想要将对象放入标准容器时,您将必须使用共享所有权的对象。 这意味着,如果您复制对象,并且原始对象超出了范围,则在所有副本都超出范围之前,不会释放所拥有的资源。 例如,考虑使用 boost::shared_ptr ,或者如果您不想编写自己的类来管理它,则可以将其包装到您的类中。
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. 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. That is, by the way, the same reason that forbids putting auto_ptr into a container.
The next C++ version, called c++0x will support those move semantics by introducing special move constructors. 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.
如果您没有对资源做任何狡猾的事情(请参阅其他评论),那么使您想要更改的成员变量可变将允许您在 const 函数中更改它。
If you're not doing anything dodgy with resources (see other comments) then making the member variable that you want to change mutable will allow you to alter it in a const function.
你无法做你想做的事。 您必须使用指针,无论是普通指针还是智能指针(但不是 auto_ptr<>)。 为什么不能使用Boost智能指针? 它们非常轻量级,并且应该可以在所有相当标准的 C++ 编译器中工作。 您不必使用所有 Boost。
You can't do what you're trying to do. You'll have to use pointers, either plain or smart (but not auto_ptr<>). Why can't you use Boost smart pointers? They're pretty lightweight, and should work in all reasonably standard C++ compilers. You don't have to use all of Boost.
根据您想要执行的操作(更多详细信息会更好),您可以在调用
push_back
之前/之后修改对象,或者编写一个简单的包装类,该包装类采用指向您的类的指针,并且可以插入到双端队列
中。 然后,该对象可以在构造/销毁等方面对您的类执行适当的操作。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 adeque
. This object can then do the appropriate thing to your class on construction/destruction/etc.