[haiku-development] Re: converting const char* to char*

  • From: Rene Gollent <anevilyak@xxxxxxxxx>
  • To: haiku-development@xxxxxxxxxxxxx
  • Date: Fri, 17 Jul 2020 13:17:11 -0400

On Fri, 17 Jul 2020 at 13:07, Suhel Mehta <mehtasuhel@xxxxxxxxx> wrote:


Hi Everyone!
While implementing code to read the directories of the UFS2 file
system I ran into a problem. I have a const char* variable that stores
a directory name and now I want to pass it as a parameter to the
GetNext function that accepts char*. After searching and testing I
have found the below code that may work

Is there a reason GetNext() can't simply be modified to take a const
char* instead? Unless it needs to modify the passed in string, there
shouldn't be any reason to remove constness here, and that's generally
safer. That having been said, in the general case, constness can be
removed in C++ via const_cast, e.g.
GetNext(const_cast<char*>(yourvar), ...); I'd advise against that
approach unless absolutely necessary though.

Best Regards,

Rene

Other related posts: