C++中"〜"(代字号)符号的含义?

Mil*_*hiz 14 c++

// 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;
 };
Run Code Online (Sandbox Code Playgroud)

我现在想要~AirlineTicket();这个代码的含义是 什么?我不知道"〜"(代字号)的含义.

Teo*_*zon 25

它是析构函数.当你破坏(到达范围的末尾,或者调用delete指针)对象的实例时,它会被调用.

  • 请注意,`~`也可以在不同的上下文中表示按位. (13认同)

Naw*_*waz 15

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

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

int a = ~100;
int b = ~a;
Run Code Online (Sandbox Code Playgroud)

输出:( ideone)

-101
100
Run Code Online (Sandbox Code Playgroud)

  • +1重要的是这个答案在这个页面上,或者OP可能已经消失了,认为波形符号 - 他之前在C++中看不到 - 仅用作析构函数名称的一部分.这不是真的.(虽然为了使它成为一个非常好的答案,你也应该谈论析构函数.) (10认同)
  • @Nawaz这不是一个转储随机相关信息的地方,答案应该解决被问到的问题,你的问题不会 - 无论你在解释中提出什么警告 (6认同)
  • @Nawaz"~AirlineTicket()的含义是什么;在这段代码中?" 没有回答"按位否定",它是一个析构函数 (5认同)
  • @Nawaz这是*字面上*问题,我引用了提问者,他们问这个代码中的含义****你的答案没有解决这个问题 (3认同)
  • @Daniel:这不是问题.问题是:C++中`~`的含义是什么?OP看到它在一个上下文中的使用,我给了他其他上下文,其中相同的符号使用具有完全不同的含义! (2认同)
  • @Daniel:这不是随机的.这是相同的标志.如果我要谈论设计模式或政治,它本来就是随机的.看来,对于你来说,事情是相同的或随机的,但不是以某种方式相关的! (2认同)
  • @Tomalak:这是非常重复的,因为几乎每个人都谈到析构函数。所以我写了别人没有写的。 (2认同)
  • +1来自我正在研究的C ++代码中寻找代字号含义的搜索。就我而言,它是位操作。在这里获得两个答案很有帮助。 (2认同)

Mah*_*esh 6

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

何时调用析构函数?

举个例子——

#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;
}
Run Code Online (Sandbox Code Playgroud)

在我的系统上的结果:

Destructor called   0xbfec7942  
Destructor called   0xbfec7943
Run Code Online (Sandbox Code Playgroud)

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

班级什么时候管理资源?

#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;
}
Run Code Online (Sandbox Code Playgroud)

在我的系统上的结果:

Destructor called   0x9b68008
Run Code Online (Sandbox Code Playgroud)

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