64,662
社区成员
发帖
与我相关
我的任务
分享
Please consider the code below:
class Buffer
{
public:
Buffer() : size(0), data(0) {}
Buffer(int s, void* d)
{
if (s <= 0) throw std::invalid_argument();
data = d;
size = s;
}
~Buffer() { delete data; }
void** operator&() { return &data; }
private:
Buffer& operator=(const Buffer&);
int size;
void* data;
}
Does this code has any restrictions, which do not allow to use objects of this class into the std containers (std:vector<Buffer>)? Is yes, please describe them.
Please look at the code:
template <typename T> void Func2(std::auto_ptr<T> v1, std::auto_ptr<T> v2, std::auto_ptr<T> v3)
{
<…>
}
template <typename T> bool Func1()
{
try
{
std::auto_ptr<T> p(new T);
Func2(p, std::auto_ptr<T> (new T), std::auto_ptr<T>(new T));
p->SomeMethod();
return true;
}
catch(const std::bad_alloc&)
{
return false;
}
}
Answer the questions:
Do you see any problems in this code?
Is this code an exception safe?
If you see any problems please mark all of them (by using the following style: line of code – description of the problem; for example: “printf(s) – uninitialized variable s”).