0
点赞
收藏
分享

微信扫一扫

C++翁恺学习07-构造和析构

萧萧雨潇潇 2022-01-18 阅读 71

point::init()

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

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();
};

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
举报

相关推荐

0 条评论