zoukankan      html  css  js  c++  java
  • C++基础学习--写写代码3

    析构函数是类的特殊成员函数,当类对象的声明周期结束时,会自动执行析构函数。析构函数并不是删除对象,而是在撤销对象所占用的内存之前完成一些清理动作,使得这部分内存可以被新对象使用。析构函数不返回任何值也没有参数,所以析构函数不能被重载,一个类可以有多个构造函数,但是只有一个析构函数。

    main.h

    class Base {
    public:
        Base(int age):m_age(age){};
        ~Base();
    private:
        int m_age;
    };
    

    main.cpp

    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include "main.h"
    
    Base::~Base()
    {
        std::cout << "this is ~Base() age =" << m_age << std::endl;
    }
    
    int main(void)
    {
        if (1) {
            Base ba(10);
        }
        Base ba(12);
        return 0;
    }
    

    运行结果:

    this is ~Base() age =10
    this is ~Base() age =12
    
  • 相关阅读:
    模型
    smarty变量
    smarty变量调节器
    分页
    表单验证(注册)
    php数据库访问
    php面向对象 继承
    php 面向对象
    php正则数组
    php 函数
  • 原文地址:https://www.cnblogs.com/wangdongfang/p/14509041.html
Copyright © 2011-2022 走看看