E
Eric Lilja
Consider this code that doesn't compile on two compilers I've tried:
void foo(const char *) {}
int main()
{
foo(new const char * ("bar"));
}
$ g++ -Wall -Wextra -std=c++98 -pedantic -g cpptest.cpp -o runme
cpptest.cpp: In function `int main()':
cpptest.cpp:7: error: cannot convert `const char**' to `const char*'
for argument `1' to `void foo(const char*)'
I came across this when I used a third party gui library and wanted to
associate a string with a gui control. The gui control stores some
integer type as its user value (that is normally a pointer) so I was
casting to that when calling my function and the compiler was happy
but I noticed the stored string was garbage when I retrieved it later.
And when I changed the signature of the function to take a pointer to
const char instead and only cast at the last possible instant I got an
error (and the explanation why the text I ended up storing was
garbage). It's just that I don't understand why that does not work.
void foo(const char *) {}
int main()
{
foo(new const char * ("bar"));
}
$ g++ -Wall -Wextra -std=c++98 -pedantic -g cpptest.cpp -o runme
cpptest.cpp: In function `int main()':
cpptest.cpp:7: error: cannot convert `const char**' to `const char*'
for argument `1' to `void foo(const char*)'
I came across this when I used a third party gui library and wanted to
associate a string with a gui control. The gui control stores some
integer type as its user value (that is normally a pointer) so I was
casting to that when calling my function and the compiler was happy
but I noticed the stored string was garbage when I retrieved it later.
And when I changed the signature of the function to take a pointer to
const char instead and only cast at the last possible instant I got an
error (and the explanation why the text I ended up storing was
garbage). It's just that I don't understand why that does not work.