vector
C ++でどのように実装されているかを理解しようとしていました。これを尋ねる以前の質問があったので、それを見て、小さな質問があります。リンクされた質問の実装が正しいと仮定して、次のコードを見てみましょう。
int main(){
Vector<int> test2 = test_Vector();
cout << test2[0] << endl;
return 0;
}
// below is NOT the STL vector object, but the one in the linked question,
// in which the asker tries to implement STL vector himself/herself
Vector<int> test_Vector(){
Vector<int> test;
test.push_back(5);
return test;
}
私が理解しているように、test
Vector
オブジェクトはローカルに作成されるため、test_Vector
メソッドが戻ると、ローカルオブジェクトはスコープ外になり、それによってデストラクタが呼び出されdelete
、動的配列が呼び出されます。コードは実際に機能し、5 が出力されるので、私は間違っていると思います。正しい説明は?