C++ 如何重载析构函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/988925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:16:03  来源:igfitidea点击:

How to overload a destructor?

c++

提问by Joshua

How do I overload a destructor?

如何重载析构函数?

回答by JaredPar

You can't. There is only one destructor per class in C++.

你不能。在 C++ 中,每个类只有一个析构函数。

What you can do is make a private destructor and then have several public methods which call the destructor in new and interesting ways.

你可以做的是创建一个私有析构函数,然后有几个公共方法以新的和有趣的方式调用析构函数。

class Foo {
  ~Foo() { ... }
public:
  DestroyFoo(int) { ... };
  DestroyFoo(std::string) { ... }
};

回答by Dima

Overloading means having several functions with the same name which take different arguments. Like swap(int &a, int &b)and swap(double &a, double &b). A destructor takes no arguments. Overloading it would not make sense.

重载意味着有几个具有相同名称的函数,它们采用不同的参数。喜欢swap(int &a, int &b)swap(double &a, double &b)。析构函数不接受任何参数。重载它没有意义。

If you need to do different things when destroying an object depending on certain circumstances, then you just need the appropriate if statements in your destructor to check for those circumstances.

如果您需要根据某些情况在销毁对象时做不同的事情,那么您只需要在析构函数中使用适当的 if 语句来检查这些情况。

回答by Wim ten Brink

Interesting question but the only reason why you'd want to overload a destructor would be because you want to free some resource in one destructor and leave it behind in another one, right?

有趣的问题,但您想要重载析构函数的唯一原因是因为您想在一个析构函数中释放一些资源并将其留在另一个析构函数中,对吗?

Basically, you can achieve such behavior from your own destructor by using an additional boolean value which would tell you if a specific resource should be freed or not. This boolean would be set in your constructor and/or one of your other methods and in your destructor you check if it's set. If it's not set, then you'd free the resource. Otherwise, you just leave the resource and probably some other task will free it. (This would make sense when you share resources between multiple objects.)

基本上,您可以通过使用额外的布尔值来从您自己的析构函数中实现这种行为,该值会告诉您是否应该释放特定资源。这个布尔值将在您的构造函数和/或您的其他方法之一中设置,并在您的析构函数中检查它是否已设置。如果未设置,则您将释放资源。否则,您只需离开资源,可能其他一些任务就会释放它。(当您在多个对象之间共享资源时,这很有意义。)

The reason why you can't overload a destructor is because your code wouldn't have a clue about which destructor it needs to call when you destroy an object. Unless you're calling destructors badly but then you're behaving badly! ;-)

不能重载析构函数的原因是您的代码不知道在销毁对象时需要调用哪个析构函数。除非你调用析构函数很糟糕,但你的行为很糟糕!;-)

回答by rlbond

You can't! Each class can only have one destructor. How could you have more than one? The destructor is triggered automatically; there's no way the language would know which one to call.

你不能!每个类只能有一个析构函数。你怎么可能有不止一个?析构函数自动触发;语言无法知道该调用哪一个。

Virtual destructors, however, are another matter.

然而,虚拟析构函数是另一回事。

回答by John Dibling

You don't. You can't have 2 destructors in one class.

你没有。一个类中不能有 2 个析构函数。

What are you trying to accomplish?

你想达到什么目的?

回答by ZenOokami

You don't overload the destructor because you never call it. That's the basic gist of it. (From what we went over in class.)

您不会重载析构函数,因为您从不调用它。这是它的基本要点。(从我们在课堂上复习的内容。)