最近的一个问题让我想知道显式复制构造函数.这是我在Visual Studio 2005下尝试编译的示例代码:
struct A
{
A() {}
explicit A(const A &) {}
};
// #1 > Compilation error (expected behavior)
A retByValue()
{
return A();
}
// #2 > Compiles just fine, but why ?
void passByValue(A a)
{
}
int main()
{
A a;
A b(a); // #3 > explicit copy construction : OK (expected behavior)
A c = a; // #4 > implicit copy construction : KO (expected behavior)
// Added after multiple comments : not an …Run Code Online (Sandbox Code Playgroud) c++ ×2