Take the 2-minute tour ×

The following code produces the compilation error, invalid conversion from 'const char*' to 'char*'. Both ptrInputFileName and ptrFileName are both declared as const char*. Any suggestions on how to get this to compile? Thank you.

TextInputBuffer::TextInputBuffer(const char *ptrInputFileName)
    : ptrFileName(new char[strlen(ptrInputFileName) + 1])
    //--Copy the file name.
    std::strcpy(ptrFileName, ptrInputFileName);
share|improve this question

3 Answers 3

up vote 5 down vote accepted

strcpy takes the the destination as non-const pointer char* and ptrFileName is const char*. No implicit conversion possible and I wouldn't recommend an explicit conversion. Just make ptrFileName non-const.

share|improve this answer
Awesome, thanks. –  Bryan Dunsmore May 29 '11 at 18:43

Obviously, you can't copy into a something pointed to by a const * - remove the const, and if that cause problems calling the constructor, you are doing something semantically invalid.

Also, your use of pseudo-hungarian makes the code difficult to read. Lose the ptr prefixes, and make the names shorter.

share|improve this answer
Thanks on the answer; on the advice though, I'm good. –  Bryan Dunsmore May 29 '11 at 18:47
The negative value of these name prefixes needs to be more widely recognized. –  Fred Nurk May 29 '11 at 18:57
@Neil: Do you feel the same way about Apps Hungarian as you do about Systems Hungarian? –  Bill May 29 '11 at 20:11
@NeilButterworth: I believe you misunderstood Bill. "Apps" vs "Systems" is the difference between unsafe_user_input and str_user_input. "Unsafe" or "row" (instead of "col") is "the type" (cf. "a category") of variables, but it was misunderstood to correspond with the typesystem of the language. The misunderstanding was widely promulgated and is what most people know. :( –  Fred Nurk May 29 '11 at 20:18
@dunsmoreb: Even MicroSoft, where Hungarian was invented, now discourages the use of Hungarian notation: msdn.microsoft.com/en-us/library/ms229045.aspx –  John Dibling May 29 '11 at 21:17

Use std::string.

struct TextInputBuffer {
  TextInputBuffer(const char *filename)
  : _filename(filename)

  // Explicit delete not even required as it would have been
  // when you used new.

  std::string _filename;
share|improve this answer

Your Answer


