C++ 中“~”(波浪号)符号的含义?

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

Meaning of "~" (tilde) symbol in C++?

c++

提问by Milad Sobhkhiz

// AirlineTicket.h

#include <string>
class AirlineTicket
{
public:
  AirlineTicket();

  ~AirlineTicket();

  int          calculatePriceInDollars();
  std::string  getPassengerName();
  void         setPassengerName(std::string inName);
  int          getNumberOfMiles();
  void         setNumberOfMiles(int inMiles);
  bool         getHasEliteSuperRewardsStatus();
  void         setHasEliteSuperRewardsStatus(bool inStatus);

 private:
   std::string  mPassengerName;
   int          mNumberOfMiles;
   bool         fHasEliteSuperRewardsStatus;
 };

I want now what is the meaning of ~AirlineTicket();in this code? I don`t know the meaning of "~" (tilde).

我现在想知道~AirlineTicket();这段代码的含义是 什么?我不知道“~”(波浪号)的意思。

回答by Teo Klestrup R?ijezon

It is the destructor. It gets called when you destroy (reaching end of scope, or calling deleteto a pointer to) the instance of the object.

它是析构函数。当您销毁(到达范围的末尾,或调用delete指向的指针)对象的实例时,它会被调用。

回答by Nawaz

In the context you're using it, it defines a destructor.

在您使用它的上下文中,它定义了一个析构函数。

In othercontext such as the following one, it's also called bitwise negation (complement):

其他上下文中,例如以下上下文,它也称为按位否定(补码):

int a = ~100;
int b = ~a;

Output: (ideone)

输出:(ideone

-101
100

回答by Glenn McAllister

It's the class destructor. You might want to pick up an introductory text on C++ development, as destructors are a fundamental concept in OOP. There is a good reference site here, and the C++ FAQis another good resource.

它是类析构函数。您可能想要阅读有关 C++ 开发的介绍性文本,因为析构函数是 OOP 中的一个基本概念。这是一个很好的参考站点这里,和C ++ FAQ是另一个很好的资源。

回答by Mahesh

~signs that it is a destructor and when ever the object goes out of scope, corresponding destructor is called.

~表明它是一个析构函数,并且当对象超出范围时,就会调用相应的析构函数。

When the destructor is called ?

何时调用析构函数?

Taking an example -

举个例子——

#include <iostream> 
class foo
{
    public:
    void checkDestructorCall() ;
    ~foo();
};

void foo::checkDestructorCall()
{
    foo objOne;   // objOne goes out of scope because of being a locally created object upon return of checkDestructorCall
}

foo:: ~foo()
{
    std::cout << "Destructor called \t" << this << "\n";
}

int main()
{
    foo obj;    // obj goes of scope upon return of main
    obj.checkDestructorCall();
    return 0;
}

Results on my system:

在我的系统上的结果:

Destructor called   0xbfec7942  
Destructor called   0xbfec7943

This example just serves to indicate when a destructor is called. But destructor is written only when the class manages resources.

此示例仅用于指示何时调用析构函数。但是析构函数只有在类管理资源时才会写入。

When class manages resources?

班级什么时候管理资源?

#include <iostream> 
class foo
{

    int *ptr;

    public:
    foo() ; // Constructor
    ~foo() ;

};

foo:: foo()
{
     ptr = new int ; // ptr is managing resources.
                     // This assignment can be modified to take place in initializer lists too.
}

foo:: ~foo()
{
    std::cout << "Destructor called \t" << this << "\n";
    delete ptr ; // Returning back the resources acquired from the free store.
                 // If this isn't done, program gives memory leaks.
}

int main()
{
    foo *obj = new foo;
    // obj is pointing to resources acquired from free store. Or the object it is pointing to lies on heap. So, we need to explicitly call delete on the pointer object.

    delete obj ;  // Calls the ~foo
    return 0;
}

Results on my system:

在我的系统上的结果:

Destructor called   0x9b68008

And in the program, you posted I don't see a need to write destructor. Hope it helps !

而在程序中,你贴出我认为不需要写析构函数。希望能帮助到你 !

回答by Sujoy

~AirlineTicket();is the destructor for the class AirlineTicket

~AirlineTicket();是 AirlineTicket 类的析构函数

see this linkfor further reference

请参阅此链接以获取进一步参考

The destructor is called when you delete the object of that class, to free any memory allocated or resources being used by the object.

删除该类的对象时会调用析构函数,以释放该对象所分配的任何内存或正在使用的资源。

回答by geekosaur

~introduces a destructor. It's used because (a) it was available, ad (b) ~is one (of several) mathematical notation for "not".

~引入析构函数。使用它是因为(a)它可用,ad(b)~是“not”的(几个)数学符号之一。