【翁恺】07-构造和析构

point::init()

c++是一个注重效率的语言,不会自己做初始化,区别于java,自己搬进来自己打扫;

vs debug模式中未初始化的内存是十六进制的“0xcd,两个构成中文“烫”;

依赖于的程序员的自觉性,素养;

定义了一个对象,对象里面的数据没有初始化。

guaranteed initialization with the constructor

  • if a class has a constructor,the compiler(编译器) automatically calls that constructor at the point an object is created,before client programmers can get their hands on the object
  • the name of the constructor is the same as the name of class

how a constructor does?

class X {
    int i;
public:
    X();
};

1. 构造函数X没有返回类型,和类名一模一样;

2. 对象被创造的时候 X() 被自动调用;

3. X() 也是类的成员函数,this指的是那个对象的地址;

X a;----->a.X()

constructors with arguments

  • the constructor can have arguments to allow you to specify how an object is created,give it initialization values,and so on

    Tree(int i){...} // 带参数构造函数
    Tree t(12);  // 12传给构造函数

the destructor

  • in C++,cleanup is as important as initialization and is there guaranteed with destructor

  • the destructor is named after the name of the class with a leading tilde (~).the destructor never has any arguments

    class Y{
    public:
        ~Y();  // 波浪号
    };

没有参数,没有返回类型;被析构之前,析构函数被调用,空间被收回。

when is a destructor called?

  • the destructor is automatically by the compiler when the object goes out of scope
  • the only evidence for a destructor call is the closing brace of the scope that surrounds the object
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

理心炼丹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值