I used to allocate memory in my C++ project with new
char* buffer = new char [size];
...
delete[] buffer;
and I'd really like to move forward and use unique_ptr
, like this
unique_ptr<char[]>buffer(new char[size]);
but then I use istream& get (char* s, streamsize n);
which takes char*
as a first argument, so what should I do? I've tried to cast types, but failed. I also know I can use vector<char>
instead of pointers, but I don't really like to use it. Thank you!
The class std::unique_ptr
has a method called get()
to access the underlying pointer: use that.
unique_ptr<char[]> buffer(new char[size]);
...
myIstream.get(buffer.get(), n);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With