std::unique_ptr
is a non-copyable object. If you need a read-only access to it, you have two (main) options:
Return a reference to unique_ptr
itself:
const std::unique_ptr<unsigned char[]>& getBuffValue() const
{
return _buff;
}
Return a const pointer to the managed array:
const unsigned char* getBuffValue() const
{
return _buff.get();
}
To assign a string to the buffer, you can do:
void setBuffValue(const std::string& str)
{
_buff = std::make_unique<unsigned char []>(str.length() + 1);
std::copy_n(str.c_str(), str.length() + 1, _buff.get());
}
Note that you have to copy the terminating null character to your buffer. Otherwise it will be almost useless for the outside world because its length will not be known to the user.
But do you really need std::unique_ptr<unsigned char[]>
? std::vector
seems to be more appropriate here.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…