That is something that always intrigued me with the C++ language. With the appearance of the "const" identifier, it was supposed to replace the use of #define statement to declare constants. But as I stated to use them, I ended up in a const char to char conversion error madness.
Recently, I stated doing in class constants like done in Java. So I decided to declare something like this.
private: const static int NB_FIELD = 22;
This works perfectly. A function returning a int can return a "const int" without any problems.
But now if I try to do the same with a string like this:
private: static const char ERRORSTR [ 6 ];
Which constains the string "Error", it will generate const char to char conversion problems like in this function.
char* EnhancedSQLobject::get_SQLname ( int index )
{
if ( index < p_nb_field && index >= 0 )
return ( p_SQLfield_ptr [ index ] . SQLname)
return ( ERRORSTR );
}
Now some people will say change the return value for const char* but I can't because if the index is valid, it will return a non-const variable, but if the index is invalid, it will return a constant error string.
So my choice are either to make the error string non-constant, or create a constant using #define statements. I remember a long time ago trying to define everything as a const char, but eventually it blew up somewhere else in the chains of conversion. Making it just easier to use regular char (the String class is not a option for me)
This is plain ridiculous, why implement a feature that only works for only a part of the variable types. I imagine it's related to the fact that stings are in fact table of characters so it probably return a pointer instead of a value.
There's a difference between the two conversions. For
int:But if you could do it for
char*:The actual equivalent to the former for pointers would be:
It's the difference between a const pointer and a pointer to const.