简体   繁体   English

C++ 中用于动态分配类型的重载 + 运算符

[英]Overloading + Operator in C++ for a Dynamically Allocated Type

I'm trying to override the + operator for a type whose constructor involves calling new (that is, the constructor needs to dynamically allocate something else) and hence whose destructor involves calling delete.我正在尝试覆盖其构造函数涉及调用 new 的类型的 + 运算符(即构造函数需要动态分配其他内容),因此其析构函数涉及调用 delete。 Somewhere, I'd like to be able to use something like:在某个地方,我希望能够使用类似的东西:

T c = a + b;

My problem is that I obviously need to create an object of type T inside the + function.我的问题是我显然需要在 + function 中创建一个 T 型的 object。 If I allocate a temporary instance of T on the stack inside the + function implementation to return by-copy, the destructor to this instance will get called as the + call exits and (or so I believe) before the assignment to c.如果我在 + function 实现内部的堆栈上分配 T 的临时实例以按副本返回,则该实例的析构函数将在 + 调用退出时被调用,并且(或者我相信)在分配给 c 之前调用。 So that's not an option.所以这不是一个选择。 My other option seems to be to use new and dereference the pointer returned by new when returning.我的另一个选择似乎是使用new并在返回时取消引用 new 返回的指针。 It seems that the problem with this approach, however, is that the pointer will be inaccessible, and there will be no way to call delete on it.然而,这种方法的问题似乎在于指针将不可访问,并且无法对其调用delete

So my question is...it can't be that overloading operators on types that involve dynamic allocation is that rare.所以我的问题是......不可能在涉及动态分配的类型上重载运算符很少见。 How do people generally handle such a situation?人们一般如何处理这种情况?

You make sure T obeys The Rule of 3 , and there are no worries.您确保 T 遵守规则 3 ,并且没有后顾之忧。

If you create a temporary object on stack within operator+ , then on exit, depending on compiler and your operator implementation:如果您在operator+内的堆栈上创建临时 object ,则在退出时,具体取决于编译器和您的操作员实现:

  1. The object will be passed via copy constructor to another temporary object, which will be passed to c via copy constructor again. object 将通过复制构造函数传递给另一个临时 object,该临时 object 将再次通过复制构造函数传递给c OR或者
  2. The object will be passed to c via copy constructor. object 将通过复制构造函数传递给c OR或者
  3. The temporary object will be actually c and there will be no copy constructor calls.临时的 object 实际上是c并且不会有复制构造函数调用。 (see http://en.wikipedia.org/wiki/Return_value_optimization ) (见http://en.wikipedia.org/wiki/Return_value_optimization

As mentioned before, if you follow The Rule of 3 and provide correct implementation of copy constructor and assignment operator there won't be any problems under any circumstances, so you don't need to worry about actual implementation (unless you're crazy about performance).如前所述,如果您遵循 The Rule of 3 并提供了复制构造函数和赋值运算符的正确实现,那么在任何情况下都不会出现任何问题,因此您无需担心实际实现(除非您为表现)。 That's all about OOP.这就是 OOP 的全部内容。

std::string and std::vector are somehow able to do that, and your class is too. std::stringstd::vector以某种方式能够做到这一点,你的 class 也是。 I don't suggest you learn their source code, it's quite intimidating.我不建议你学习他们的源代码,这很吓人。 As another poster said, the rule of 3 is your friend.正如另一位海报所说,3 规则是你的朋友。 I can add that you should not call new in your function.我可以补充一点,你应该在你的 function 中调用new Allocate on the stack and return by value.在堆栈上分配并按值返回。 C++ will do the necessary magic. C++ 将发挥必要的作用。

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

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