私は次のC++のコードを持っている:キャスト++タイプ
#include <iostream>
#include <string>
int main(int argc, char* argv[])
{
const std::string s1 = "ddd";
std::string s2(std::string(s1));
std::cout << s2 << std::endl;
}
は、結果は次のとおりです。なぜ? 私は-Wallフラグ、コンパイラの書き込み警告を使用する場合:
#include <iostream>
#include <string>
int main(int argc, char* argv[])
{
const std::string s1 = "ddd";
std::string s2((std::string)(s1));
std::cout << s2 << std::endl;
}
:常に
'真' しかし、このコードとして評価します 'のstd ::文字列s2(STD ::文字列)' のアドレスを結果: DDD
それは正常な結果
は、あなただけの二行目 'のstd ::文字列s2(S1)を作ることができませんでした;'なぜあなたがする必要がありますか2番目の 'std :: string'を含めますか? – rhololkeolke
中間コピーを作成するのはなぜですか?どうして 'std :: string s2 = s1;'? –
@Oscar:あなたのバージョンでは、中間のデフォルトの初期化変数が作成され、次にコピー代入演算子を使用してコピーされます。中間のものがない正しいバージョンはrhoです。 – Xeo