コピーコンストラクタについて
Posted: 2010年6月23日(水) 00:38
はじめまして,小太鼓と申します.
C++の質問してもいいですよね・・・・?
ちょっとした疑問なんですけど,
以下のコードで,パターン1,2の結果が得られました.
#対応するコードはコメントアウトしてあります.
パターン1ではoperator=関数が呼び出されていますが,
パターン2では呼び出されていません.
それなのに,tttを出力すると期待通りの値が入力されています.
operator=が呼び出されてないならコピーコンストラクタかなと思ったんですが,
コピーコンストラクタはtttに関して呼び出されていません.
パターン2でtttに値がいつ入力されたのかわからない気持ち悪さが残っています.
ご協力願います.
パターン1
argument : Hello 0xbfdf6f98
argument : World 0xbfdf6f90
copy[Hello] 0xbfdf6f88
default[nul[/url] 0xbfdf6f84
--operator--
argument : foobar 0xbfdf6f80
0xbfdf6f98+0xbfdf6f90
/--operator--
operator= HelloWorld
destruct[HelloWorld] 0xbfdf6f80
output HelloWorld 0xbfdf6f84
destruct[HelloWorld] 0xbfdf6f84
destruct[Hello] 0xbfdf6f88
destruct[World] 0xbfdf6f90
destruct[Hello] 0xbfdf6f98
パターン2
argument : Hello 0xbf89cad8
argument : World 0xbf89cad0
copy[Hello] 0xbf89cac8
--operator--
argument : foobar 0xbf89cac4
0xbf89cad8+0xbf89cad0
/--operator--
output HelloWorld 0xbf89cac4
destruct[HelloWorld] 0xbf89cac4
destruct[Hello] 0xbf89cac8
destruct[World] 0xbf89cad0
destruct[Hello] 0xbf89cad8
C++の質問してもいいですよね・・・・?
ちょっとした疑問なんですけど,
以下のコードで,パターン1,2の結果が得られました.
#対応するコードはコメントアウトしてあります.
パターン1ではoperator=関数が呼び出されていますが,
パターン2では呼び出されていません.
それなのに,tttを出力すると期待通りの値が入力されています.
operator=が呼び出されてないならコピーコンストラクタかなと思ったんですが,
コピーコンストラクタはtttに関して呼び出されていません.
パターン2でtttに値がいつ入力されたのかわからない気持ち悪さが残っています.
ご協力願います.
パターン1
argument : Hello 0xbfdf6f98
argument : World 0xbfdf6f90
copy[Hello] 0xbfdf6f88
default[nul[/url] 0xbfdf6f84
--operator--
argument : foobar 0xbfdf6f80
0xbfdf6f98+0xbfdf6f90
/--operator--
operator= HelloWorld
destruct[HelloWorld] 0xbfdf6f80
output HelloWorld 0xbfdf6f84
destruct[HelloWorld] 0xbfdf6f84
destruct[Hello] 0xbfdf6f88
destruct[World] 0xbfdf6f90
destruct[Hello] 0xbfdf6f98
パターン2
argument : Hello 0xbf89cad8
argument : World 0xbf89cad0
copy[Hello] 0xbf89cac8
--operator--
argument : foobar 0xbf89cac4
0xbf89cad8+0xbf89cad0
/--operator--
output HelloWorld 0xbf89cac4
destruct[HelloWorld] 0xbf89cac4
destruct[Hello] 0xbf89cac8
destruct[World] 0xbf89cad0
destruct[Hello] 0xbf89cad8
#include<iostream>
#include<string>
class Test {
public:
Test() : str("null") {std::cout << "default[" << str << "] " << this << std::endl;}
Test(std::string _str) : str(_str) {std::cout << "argument : " << str << " " << this << std::endl;}
~Test() {std::cout << "destruct[" << str << "] " << this << std::endl;}
Test(const Test& t) {str = t.string(); std::cout << "copy[" << str << "] " << this << std::endl;}
std::string string() const {return str;}
void set(std::string _str) {str = _str;}
void operator=(Test t) {
str = t.string();
std::cout << "operator= " << t.string() << std::endl;
}
private:
std::string str;
};
Test operator+(Test& str_a, Test& str_b) {
std::cout << "--operator--" << std::endl;
Test t("foobar");
std::cout << &str_a << "+" << &str_b << std::endl;
t.set(str_a.string() + str_b.string());
std::cout << "/--operator--" << std::endl;
return t;
}
int main(int argc, char const* argv[/url]) {
Test t("Hello"), tt("World");
Test t2 = t;
// パターン1
// Test ttt;
//
// ttt = t + tt;
// パターン1終
// パターン2
// Test ttt = t + tt;
// パターン2終
std::cout << "output " << ttt.string() << " " << &ttt << std::endl;
return 0;
}