Reputation: 354
I have a class like the following (I've deleted extraneous items):
class GL_Texture
{
public:
GL_Texture();
private:
char* fileLocation;
};
It's constructor looks like this:
GL_Texture::GL_Texture()
{
fileLocation = ""; // error C2440: '=': cannot convert from 'const char [1]' to 'char *'
}
The constructor is producing an error error C2440: '=': cannot convert from 'const char [1]' to 'char *'
. Even if I try to make fileLocation
a char* const
, I get the following error on that line
error C2166: l-value specifies const object
If I cannot assign a string literal to a char*, and I can't qualify fileLocation
as pointer to const, how am I supposed to initialize it in the constructor?
Upvotes: 2
Views: 2680
Reputation: 9078
You have a few choices:
First, the one most people will agree is the best in most situations: don't use char *
, use std::string
.
Second, initialize to nullptr
instead.
Third: fileLocation = strdup("");
and then clean it up in the destructor. This is ugly.
Fourth: change it to char const *
.
Upvotes: 1