How many ways are there to initialize variables? Don't forget to watch out for bugs that look like variable initialization, but aren't.
Problem
What is the difference, if any, between the following?
SomeType t = u;
SomeType t(u);
SomeType t();
SomeType t;
Solution
Taking them in reverse order:
SomeType t;
The variable t is initialised using the default ctor SomeType::SomeType().
SomeType t();
This was a trick; it might look like a variable declaration, but it's a function declaration for a function t that takes no parameters and returns a SomeType.
SomeType t(u);
This is direct initialisation. The variable t is initialised using SomeType::SomeType(u).
SomeType t = u;
This is copy initialisation, and the variable t is always initialised using SomeType's copy ctor. (Even though there's an "=" there, that's just a syntax holdover from C... this is always initialisation, never assignment, and so operator= is never called.)
Semantics: If u also has type SomeType, this is the same as "SomeType t(u)" and just calls SomeType's copy ctor. If u is of some other type, then this is the same as "SomeType t( SomeType(u) )"... that is, u is converted to a temporary SomeType object, and t is copy-constructed from that.
Note: The compiler is actually allowed (but not required) to optimize away the copy construction in this kind of situation. If it does optimize it, the copy ctor must still be accessible.
[Guideline] Prefer using the form "SomeType t(u)". It always works wherever "SomeType t = u" works, and has other advantages (for instance, it can take multiple parameters).
中文:
到底有多少种方法初始化变量呢?请小心提防那些看起来好像是初始化变量,但是实际上却不是的问题。
问题:
下面的代码的写法有什么不同吗?如果不同,那么不同点是什么呢?
SomeType t = u;
SomeType t(u);
SomeType t();
SomeType t;
答案:
OK,下面我从最后一个说起吧:
SomeType t;
变量 t 使用默认的构造函数SomeType::SomeType()来初始化。
SomeType t();
小心,不要上当哦,它看起来像是一个变量 t 的声明,但是,实际上,它是函数 t() 的声明。
SomeType t(u);
这个是用 u 来初始化变量 t ,调用的函数是SomeType::SomeType(u)。
SomeType t = u;
嘿,你可不要以为这是个赋值哦,它实际上调用的是SomeType拷贝构造,用 u 来构造变量 t 。就算你在你的类里定义了operator=它也永远不会调用它的。如果你没有定义拷贝构造函数的话,程序编译就会失败的。
语义:假如 u 是SomeType类型的变量,那么"SomeType t(u)" 的调用实际上就是调用了SomeType的拷贝构造;否则,调用的就是"SomeType t( SomeType(u) )",在这里 u 先被转换成一个临时的SomeType对象,然后再通过它来拷贝构造 t 。
注意:实际上在这种情况下,编译器是会对拷贝构造进行优化的。如果编译器进行了优化,那么拷贝构造函数必须是可以被访问的,否则就会编译不通过了。
准则:最好是使用"SomeType t(u)"这种方式来构造一个变量,因为,它在"SomeType t = u"可以工作的时候都可以工作,同时它还有其他的一些优点,比如它可以接收多个参数等等。