#include <iostream>
#include <algorithm>
#include <boost/ptr_container/ptr_vector.hpp>
class Foo {
public:
int a_;
Foo(int i) : a_(i) { std::cout << "Foo()" << std::endl; }
~Foo() { std::cout << "~Foo()" << std::endl; }
};
void show( const Foo& f ) {
std::cout << f.a_ << ",";
}
int main() {
{
boost::ptr_vector<Foo> v;
for( int i = 0; i < 10; ++i ) {
v.push_back( new Foo(i) );
}
std::for_each(v.begin(), v.end(), show );
std::cout << std::endl << "scope exit..." << std::endl;
}
std::cout << std::endl << "out of scope..." << std::endl;
return 0;
}
実行結果
Foo()
Foo()
Foo()
Foo()
Foo()
Foo()
Foo()
Foo()
Foo()
Foo()
0,1,2,3,4,5,6,7,8,9,
scope exit...
~Foo()
~Foo()
~Foo()
~Foo()
~Foo()
~Foo()
~Foo()
~Foo()
~Foo()
~Foo()
out of scope...
細かいメタ関数を用意しなくて済むので、便利だ。
0 件のコメント:
コメントを投稿