template
class Foo
{
public:
Foo(T T_val);
Foo& operator=(const Foo& f);
virtual ~Foo();
const T& getTval();
private:
const T T_val;
};
template
Foo::Foo(T T_val = NULL):T_val(T_val)
{
;
}
template
Foo::~Foo()
{}
template
Foo& Foo::operator=(const Foo& f)
{
return(*new Foo(f));
}
template
const T& Foo::getTval()
{
return this->T_val;
}
int main()
{
typedef FooFoo_int;
typedef vectorFoo_vec;
Foo_vec foo_vec(10,2);
Foo_int f(4);
foo_vec.insert(foo_vec.begin(),f);
for(Foo_vec::iterator iter = foo_vec.begin();iter !=foo_vec.end();iter++)
{
cout<<iter->getTval()<<endl;
}
}
//想問一下等號運算符重載函數會不會導致內存洩漏,有沒有更好的寫法
會的!!!!!!!!!!!!!