Reputation: 3694
The following code can't compile:
char** s;
const char** s2 = s;
I know the followings can work but I think there should be some better legitimate way to do it?
long long x = reinterpret_cast<long long>(s);
s2 = reinterpret_cast<const char**>(x);
The background of this question is, I want to use two libraries which have functions handling argc/argv with different signatures:
void Init1(int argc, char** argv);
void Init2(int argc, const char** argv);
Then what's the best way to define my signature of main to call both of those two functions?
Upvotes: 2
Views: 2570
Reputation: 133609
In C++ you can add or remove const
-ness through const_cast
, eg:
char** s;
const char** t = const_cast<const char**>(s);
Since you are dealing with argv it should be quite safe to add const-ness to them as long as the Init2
doesn't try to alter anything, and it shouldn't since its argument is const char**
.
Mind that attepting to modify the value of a variable that was const
after removing the cv qualifier leads to undefined behavior.
Upvotes: 3