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

3万+

被折叠的 条评论
为什么被折叠?



