在 C++ 中,函数名称前的波浪号“~”表示什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1395506/
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 19:50:58  来源:igfitidea点击:

In c++ what does a tilde "~" before a function name signify?

c++

提问by Monte Hurd

 template <class T>
 class Stack
 {
 public:
    Stack(int = 10) ; 
    ~Stack() { delete [] stackPtr ; }  //<--- What does the "~" signify?
    int push(const T&); 
    int pop(T&) ;  
    int isEmpty()const { return top == -1 ; } 
    int isFull() const { return top == size - 1 ; } 
 private:
    int size ;  
    int top ;  
    T* stackPtr ;  
 } ;

回答by inanutshellus

It's the destructor, it destroys the instance, frees up memory, etc. etc.

它是析构函数,它销毁实例,释放内存等。

Here's a description from ibm.com:

以下是来自 ibm.com 的描述:

Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed. A destructor is called for a class object when that object passes out of scope or is explicitly deleted.

析构函数通常用于在对象被销毁时释放内存并为类对象及其类成员进行其他清理。当该对象超出范围或被显式删除时,会为该类对象调用析构函数。

See https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_74/rzarg/cplr380.htm

请参阅https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_74/rzarg/cplr380.htm

回答by dmckee --- ex-moderator kitten

As others have noted, in the instance you are asking about it is the destructor for class Stack.

正如其他人所指出的,在您询问的实例中,它是class Stack.

But taking your question exactly as it appears in the title:

但是完全按照标题中显示的方式回答您的问题:

In c++ what does a tilde “~” before a function name signify?

在 C++ 中,函数名称前的波浪号“~”表示什么?

there is another situation. In any context exceptimmediately before the name of a class (which is the destructor context), ~is the one's complement (or bitwise not) operator. To be sure it does not come up very often, but you can imagine a case like

还有另一种情况。在任何上下文中,除了紧接在类名之前(这是析构函数上下文),~是一个的补码(或按位非)运算符。可以肯定的是,它不会经常出现,但是您可以想象这样的情况

if (~getMask()) { ...

which looks similar, but has a very different meaning.

这看起来很相似,但具有非常不同的含义。

回答by Samuel Danielson

It's a destructor. The function is guaranteed to be called when the object goes out of scope.

这是一个破坏者。当对象超出范围时,保证会调用该函数。

回答by Klaim

This is a destructor. It's called when the object is destroyed (out of life scope or deleted).

这是一个析构函数。当对象被销毁(超出生命范围或删除)时调用它。

To be clear, you have to use ~NameOfTheClass like for the constructor, other names are invalid.

需要明确的是,您必须像构造函数一样使用 ~NameOfTheClass,其他名称无效。

回答by Pierre

It's the destructor. This method is called when the instance of your class is destroyed:

这是析构函数。当您的类的实例被销毁时,将调用此方法:

Stack<int> *stack= new Stack<int>;
//do something
delete stack; //<- destructor is called here;

回答by maxfridbe

That would be the destructor(freeing up any dynamic memory)

那将是析构函数(释放任何动态内存)