I have a warning that I can't remove...it tells me "deprecated conversion from string constant to char*". The code where it complains is the following:
class THREE_DATA_4_COMPUTE
{
public:
THREE_DATA_4_COMPUTE(const QString &cn, char* umn = "", const QString &un = (const QString)"")
{
this->curveName = cn;
this->unitModuleName = umn;
this->unitName = un;
};
QString curveName;
char* unitModuleName;
QString unitName;
protected:
THREE_DATA_4_COMPUTE(){};
};
and I call it as follows, passing only 1 parameter (hence I need the defaults):
THREE_DATA_4_COMPUTE *aux = new THREE_DATA_4_COMPUTE(this->getRmft());
What is wrong?. I am with c++, Qt 4.8 and linux.
Those two mistakes:
""
is not a string variable but a constant, just as"hola"
and therefore such a string should only be pointed to from a constant pointerQString
from a string because you already have a constructor for itHow should that constructor look like?
One problem you may run into when trying this signature is that
umn
it is now not compatible withunitModuleName
, since one isconst char*
and the otherchar*
. In my opinion, unless strictly necessary, I would convertunitModuleName
toQString
... although the same goes for changingunitModuleName
to beconst char*
... since you don't show the implementation of the class it is not clear if that value can change or not.Of course, since it's free to put longer variable names, for readability I'd almost advise you to make variable names more descriptive.
Problem.
The alarm
deprecated conversion from string constant to char*
is usually given when trying to store a text literal whose type is a constant pointer tochar
, in a variable that is a pointer tochar
(without being a constant).You may also encounter this alarm when trying to store a constant
char
pointer to achar
non-constant pointer to.Examples.
In the code above we see how a text literal (
"Patatas fritas, que ricas!"
) is stored in a variable of type pointer tochar
without the qualifierconst
. The type of the text literal isconst char[27]
(constant array of 27 elements of typechar
), this type of array is implicitly convertible to a constant pointer tochar
; when passing it to a non-constant pointer, the alarm appears. To correct the problem add the qualifierconst
to the parameter:It can also happen if the return of a function is
const char *
and is stored in a pointer variable tochar
that does not have the qualifierconst
:Why does it happen?: Compatibility with c
The implicit conversion of
const char *
achar *
is the only such conversion allowed in C++, it exists for backward compatibility with C.In the C language the semantics of the text literals and the qualifier
const
are different than in C++. In C a text literal is an array ofchar
non-constants while in C++ a text literal is an array ofchar
constants. To allow the same semantics in C as in C++, this conversion was marked as an alarm instead of an error.So in C passing a text literal to a function is allowed:
However, conversions from qualified data
const
to unqualified data are explicitly prohibited in C++:While with we get the deprecated
char
alarm with we get an error. If you use newer C++ compilers you will get an error instead of an alarm.int