C 和 C++ 的变量初始化
As a general rule, a base class that defines one or more virtual functions should always define a virtual destructor.
The exception declaration
catch(bad_alloc)
does not declare an exception object because we are interested only in catching the exception type and not in actually manipulating the object within the catch clause. To suppress the throw of the bad_alloc exception, we can writeptext = new (nothrow) vector<string>;
If the new expression fails, it returns 0. Any use of ptext must first check that it is not zero.
永远不使用 =
初始化。有构造函数的类型使用 ()
,
没有的使用 {}
.
因为 =
会默认先调用 ()
或 {}
初始化,
但是会在匹配失败后进行隐式转换,因此受到 explicit
影响.
C++ 对象的初始化在构造函数们被自动调用之前. 因此构造函数其实仅仅是一个对象被创建时候调用的普通函数. 如果类中包含特殊对象不能或不想在执行构造函数前使用默认初始化,可以使用 初始化列表 减轻这个问题.
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Comment
GitalkValine