美文网首页
C++ 析构函数

C++ 析构函数

作者: samtake | 来源:发表于2019-10-09 02:54 被阅读0次

析构函数(destructor)是一种特殊的成员函数。

  • 类的析构函数名是在类名前加一个波浪好~
  • 累的析构函数是在删除对象的时候被隐式调用
  • 析构函数没有形参页不反悔任何值
  • 析构函数必须是 public类型的
CreateAndDestructor::CreateAndDestructor(int ID, string msg){
    objectID = ID;
    message = msg;
    
    cout << "Object " << objectID << " 构造函数 runs  " <<message << endl;
}


void create(void){
    cout << "createFunc " << endl;
    CreateAndDestructor fifth(5, "(local automatic in create)");
    static CreateAndDestructor sixth(6, "(local automatic in create)");
    CreateAndDestructor seventh(7, "(local automatic in create)");
}

CreateAndDestructor::~CreateAndDestructor(){
    cout<<(objectID ==1 || objectID == 6 ? "\n" : "");
    
    cout << "Object " << objectID << " 析构函数 runs   " <<message << endl;
}


void testCreateAndDestructor(){
    cout << "testFunc " << endl;
    CreateAndDestructor second(2, "(local automatic in testFunc)");
    static CreateAndDestructor third(3, "(local automatic in testFunc)");
    
    create();
    
    
    cout << "testFunc : " << endl;
    
    CreateAndDestructor forth(4, "(local automatic in testFunc)");
    
    cout << "\ntestFunc end \n" << endl;
}

testFunc 
Object 2 构造函数 runs  (local automatic in testFunc)
Object 3 构造函数 runs  (local automatic in testFunc)
createFunc 
Object 5 构造函数 runs  (local automatic in create)
Object 6 构造函数 runs  (local automatic in create)
Object 7 构造函数 runs  (local automatic in create)
Object 7 析构函数 runs   (local automatic in create)
Object 5 析构函数 runs   (local automatic in create)
testFunc : 
Object 4 构造函数 runs  (local automatic in testFunc)

testFunc end 

Object 4 析构函数 runs   (local automatic in testFunc)
Object 2 析构函数 runs   (local automatic in testFunc)

Object 6 析构函数 runs   (local automatic in create)
Object 3 析构函数 runs   (local automatic in testFunc)
Program ended with exit code: 0
  • 在全局作用域内定义的对象的构造函数在该文件中的任何其他函数(包括main函数)开始执行之前执行。
  • main函数终止时,调用响应的析构函数。exit函数强制程序立即终止并且不执行自动对象的析构函数。
  • 自动局部对象的 析构函数在执行到达定义该对象的程序点是调用,对应的析构函数是在该对象离开该对像所在的作用域时调用。
  • static局部对象的析构函数只在执行第一次到达定义对象的程序点时调用一次,对应的析构函数是在main函数终止时或者exit函数时调用。

相关文章

  • 简介python中的析构函数与构造函数

    python的构造和析构函数为固定的名字。 构造函数 析构函数 不像c++中那样构造函数和析构函数是类名字。并且在...

  • [C++之旅] 13 析构函数

    [C++之旅] 13 析构函数 析构函数与构造函数相反,构造函数在实例化一个对象时调用,而析构函数在销毁一个对象时...

  • 2020-02-10 C++基础2

    1:为什么析构函数必须是虚函数?为什么C++默认的析构函数不是虚函数? 将可能会被继承的父类的析构函数设置为虚...

  • c++构造函数,析构函数,调用顺序

    最近,在看c++,聊一个简单的话题吧。构造函数,析构函数,及调用顺序。 构造函数, 构造函数,析构函数,如上代码所...

  • c++学习笔记2(GeekBand)

    拷贝构造、拷贝赋值和析构 c++中有Big Three三个特殊的函数,他们就是拷贝构造函数,拷贝赋值函数和析构函数...

  • 第十五章 析构过程

    c++中,如果没有定义析构函数,c++会提供一个默认析构函数;由于swift采用自动引用计数来进行内存管理,不需要...

  • 2002.C++BASE-构造函数、析构函数

    转:C++继承中构造函数、析构函数调用顺序及虚析构函数 1.构造函数 大家都知道构造函数里就可以调用成员变量,而继...

  • C++ 多态性 虚函数、抽象类(二)

    注意:本文中代码均使用 Qt 开发编译环境 在C++中不能声明虚构造函数,但是可以声明虚析构函数。析构函数没有类型...

  • C++ 析构函数

    析构函数是C++中,类的一个后置函数,默认自动调用,具体的实现用法请看下面。 你看,析构函数的定义...

  • Python:重载构造方法

    对于使用过C++的人来说,构造函数与析构函数不会陌生。构造函数在对象创建时被调用,析构函数在对象被销毁时被调用。而...

网友评论

      本文标题:C++ 析构函数

      本文链接:https://www.haomeiwen.com/subject/kcdvyctx.html