1)
const char*&
is what you'd want in that case, from the sounds of it. The errors you were getting were probably ambiguity errors, just like your other problem. Although for this particular function, you probably just want
char*&
(no const qualifiers)
2) nonconst pointers like
char*
can be (and are) implicitly cast to const pointers like
const char*
. Therefore if you have the following:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
void func(char* a); // "NC" version
void func(const char* a); // "C" version
//-----------
const char* Cptr = "foobar";
char* NCptr = new char[10];
func(Cptr); // this is not ambiguous. it will call the C version because a const pointer
// cannot be implicitly cast to a nonconst pointer
func(NCptr); // this, however, IS ambiguous, because the NCptr could fill either functions
// parameter requirements. Therefore the compiler will error
|
You're having this same problem, but is obfuscated by the addition of
references, which compound ambiguity issues:
1 2 3 4 5 6 7
|
void func(int a);
void func(int& a);
//-------------------
int foo;
func(foo); // ambiguous! Could be calling either!
|
You're sort of merging together these two ambiguity problems into one gigantic problem.
The easiest ways around this are any (or a combination) of the following:
1) give your functions different names
2) don't have nonconst (or const) overloads if you don't need them
3) do a better job of organizing
As it stands, if your function
get1stChange2nd
does what the name implies, you have the following:
- reads from the string buffer AND writes to it (char*, nonconst)
- needs to modify the passed pointer, so pass by reference (char*&)
Based on that criteria, I see no way to make this function do the appropriate job with any parameter type other than
char*&
(or a similar type like char**). Therefore that should be the only version of the function you have for this job, and you should remove the other ones, since they wouldn't work anyway (the const qualifier would not let them do their whole job).