std::vectorとコピーコンストラクタ
const使わないとvectorに怒られる。
#include <vector> #include <iostream> using namespace std; struct A { double x; A () : x(0.0) {} A (const A &a) { cout << "copy constructor of A" << endl; x = a.x; } }; int main () { A a; vector<A> as1, as2; cout << "as1.push_back(a);" << endl; as1.push_back(a); cout << "as2 = as1;" << endl; as2 = as1; return 0; }
実行。
:!g++ % :!./a.out as1.push_back(a); copy constructor of A as2 = as1; copy constructor of A
std::vector
そして(初期化代入ではなく)通常の代入文でTのコピーコンストラクタを呼んでいる。これはなんか違和感がある。
bits/stl_vector.hでも読むか……