我如何允许一个配有复印件的班级,该班的复印件用灯片复制?
其背景是:
I have a function that should return a list of pointers to objects that all inherit from Base, so I need something like vector<Base*>
.
Given that vector<auto_ptr>
is not much of an option, I wanted to write a simple wrapper around vector<Base*>
that deletes all elements in its destructor.
我面临以下问题:
My class has a copy constructor as follows:
auto_list(auto_list& rhs);
因此,我可以把点人名单抄送新案件,并在旧案件中予以澄清。
But obviously, that won t work with return values because temporaries don t bind to a non-const reference. Seeing that auto_ptr can be returned from functions, how did they implement it?
注:我可以使用C++11或加强,因此,移动语或独一无二的字体不是选择。
如果能够提供帮助,这是我迄今为止的法典:
template <typename T> class auto_list
{
private:
vector<T*> pointers;
public:
auto_list(vector<T*>& pointers)
{
this->pointers = pointers;
}
auto_list(auto_list& rhs)
{
this->pointers = rhs.pointers;
rhs.pointers.clear();
}
~auto_list()
{
for(typename vector<T*>::const_iterator it = this->pointers.begin(); it != this->pointers.end(); it++)
{
delete (*it);
}
}
auto_list& operator=(auto_list& rhs)
{
this->pointers = rhs.pointers;
rhs.pointers.clear();
}
vector<T*> get_pointers() const
{
return this->pointers;
}
};