ということをずっと教えられてきました
1. Class c(arg);
と
2. Class c = arg;
は 2 つの完全に同等のステートメントですが、この状況を見てください。
#include <iostream>
class Intermediary {
};
class Left {
public:
Left(const Intermediary &) {
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
};
class Right {
public:
// The argument is there just so that the example can work, see below
Right(int) {
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
operator Intermediary () const {
std::cout << __PRETTY_FUNCTION__ << std::endl;
return Intermediary();
}
};
今私がこれを行うと:
Left l = Right(0);
コンパイラは文句を言います
error: conversion from Right to non-scalar type Left requested
しかし、私がこれを行うと:
Left l(Right(0));
次に、すべてがコンパイルされ、出力は
Right::Right(int)
Right::operator Intermediary() const
Left::Left(const Intermediary&)
ただし、これを行うと:
Left l = (Intermediary)Right(0);
その後、すべてが再びコンパイルされ、出力は上記のようになります。
だから明らかに
1. Class c(arg);
と
2. Class c = arg;
は同じではありませんが、なぜ違うのでしょうか?違いは何ですか? これについてはオンラインで何も見つかりませんでした。