栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++翁恺学习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 objectthe 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 scopethe only evidence for a destructor call is the closing brace of the scope that surrounds the object

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/709721.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号