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