Cprogramming.comのランダムページを辿って、Constructors and Destructorsチュートリアル/サンプルページに気付いただけです。私は魔法this
ポインタでコンストラクタの良い昔ながらの定義を使用してきたこのコンストラクタ定義の利点は何ですか
class String
{
private:
char *str;
int size;
public:
String() : str(NULL), size(0) { } // <- This statement
String(int size) : str(NULL), size(size) { // <- And this one
str = new char[size];
}
}
:彼らは、コンストラクタを定義する以下の方法を使用している
String() {
this->str = NULL;
this->size = 0;
}
String(int size) {
this->size = size;
this->str = new char[size];
}
任意の付加的な利点があります明白な小さなコード(行数が少ない)の横にある最初の宣言では?
PS:最後にC++で何かを書いてからかなりの年月が経ちました。
http://stackoverflow.com/questions/926752/why-should-i-prefer-to-use-member-initialization-list –