I try with QByteArray to get a pointer to data using data() , swears:

 ошибка: invalid conversion from 'const char*' to 'char*' [-fpermissive] char * TempPtrBuff = ARes.data(); 

The documentation says that char * returned.

Why can an error occur?

  • Try const char * TempPtrBuff = ARes.data(); or char * TempPtrBuff = const_cast<char*>ARes.data(); depending on what you want to get. - Embedder

2 answers 2

The output of QByteArray::data() depends on the constancy of the method in which the designated function is called. Simply put:

 void MyClass::method() { char *data = ARes.data(); } void MyClass::method() const { const char *data = ARes.data(); } 

Accordingly, if the output of QByteArray::data() is different from the desired one, then change the constancy of the method or implement a type conversion, for example, with the same const_cast<T>() .

  • @ KonstantinFomin, please. - alexis031182
  • If the ARes member ARes declared as mutable in MyClass , then a non-constant version of data() will be called despite the presence of const in the method . - αλεχολυτ
  • "depends on the constancy of the method"? It does not depend on the "constancy of the method" (especially since we do not see any method in question), but on the constancy of the ARes object. If ARes constant, the constant version of the method will be called (and vice versa). - AnT
  • @alexolut, thanks, did not know. - alexis031182
  • @AnT, a question about Qt, and therefore assume that the problem in the method was taken for granted. Another thing is that probably it was necessary to somehow indicate this fact in the answer. With regards to the constancy of the ARes object, this is yes, but I assumed that the author’s problem is less likely to have this problem. - alexis031182

QByteArray::data() has an overload due to the constancy of the QByteArray object for which this function is called.

If the context of using the result of a call to data() implies the possibility of changing the data through the received pointer, then you need to use the non-constant QByteArray object and use the char* type for the variable. If this causes an error as in your question, then you are going to change the data of a constant object. This indicates an error in the design of the enclosing class (assuming that QByteArray in your case a member of the class).

If the result of data() not supposed to be changed, then you can ignore the constancy of the QByteArray object, but simply use const char* for the variable.

Attempting to remove constancy via const_cast can end badly (UB) if the object used is really constant.

An example relevant to any type of T :

 T obj; const T& cref = T; T& ref = const_cast<T&>(cref); // OK, т.к. cref реально ссылается на неконстантный объект const T obj; const T& cref = T; T& ref = const_cast<T&>(cref); // Плохо, т.к. cref реально ссылается на константный объект 

An attempt to modify obj through the ref reference in the second case leads to undefined behavior.

Another way to avoid an error in the TempPtrBuff initialization TempPtrBuff is to use auto :

 auto TempPtrBuff = ARes.data(); 

But it still does not give the ability to modify a constant object.