I’m receiving a c-string as a parameter from a function, but the argument I receive is going to be destroyed later. So I want to make a copy of it.
Here’s what I mean:
class MyClass
{
private:
const char *filename;
public:
void func (const char *_filename);
}
void MyClass::func (const char *_filename)
{
filename = _filename; //This isn't going to work
}
What I want to achieve is not simply assign one memory address to another but to copy contents. I want to have filename as “const char*” and not as “char*”.
I tried to use strcpy but it requires the destination string to be non-const.
Is there a way around? Something without using const_cast on filename?
Thanks.
Use a
std::stringto copy the value, since you are already using C++. If you need aconst char*from that, usec_str().