void* to QString C++
-
I have following code:
void check() { QString stringdat = "Hello Qt"; foo(&stringdat); } void foo(void* ptr) { QString* data = (QString*) ptr; if(data != NULL) { qDebug() << "Data is " << data; } }However the challenge is :
QString* data = (QString*) ptr;How do it achieve the void* to QString conversion ?
-
I have following code:
void check() { QString stringdat = "Hello Qt"; foo(&stringdat); } void foo(void* ptr) { QString* data = (QString*) ptr; if(data != NULL) { qDebug() << "Data is " << data; } }However the challenge is :
QString* data = (QString*) ptr;How do it achieve the void* to QString conversion ?
@derric said in void* to QString C++:
QString* data = (QString*) ptr; qDebug() << "Data is " << data;As @eyllanesc has said: since
datais aQString *, if you output just that you get a pointer value, like0x65fe9c. If you want to see what it points to, you needqDebug() << "Data is " << *data;, which dereferences the pointer and will print theQString, which isHello Qthere.There is nothing special in this context because you chose to pass the (address of the) original
QStringas avoid *here.As I'm sure you are aware, if you use that C-style cast
(QString*) ptrand theptrpassed in does not point to aQString, disaster will occur when you go*data! :) -
Hi and welcome to devnet,
You need to dereference the pointer.
-
void pointers are highly discouraged in C++...just sayin. Look at the plethora of "safe coding" standards out there and you'll see what I mean. I'd suggest either using the type system or making templates and avoiding void* altogether.
Your example use case is a prime example of why it's bad ju-ju. What happens when the internal foo() QString tries to reference a pointer that is NOT a conforming QString?
-
void pointers are highly discouraged in C++...just sayin. Look at the plethora of "safe coding" standards out there and you'll see what I mean. I'd suggest either using the type system or making templates and avoiding void* altogether.
Your example use case is a prime example of why it's bad ju-ju. What happens when the internal foo() QString tries to reference a pointer that is NOT a conforming QString?
@Kent-Dorfman I have posted the above for a reason and i needed to know how? i'm expecting answers not suggestions or questions back.
If you could put the question about what happens when the internal QString tries to reference a pointer thats is not QString at https://forum.qt.io/, i'll be happy to answer your question.
Here i'm not looking forward for why i should not be using void pointers
-
@SGaist Thank you. But my point is i have de referenced it at function call as below:
foo(&stringdat);The output i get is as follows :
Data is 0x65fe9cHowever i was expecting
Data is Hello QtPlease help me out here. I'm new
-
I have following code:
void check() { QString stringdat = "Hello Qt"; foo(&stringdat); } void foo(void* ptr) { QString* data = (QString*) ptr; if(data != NULL) { qDebug() << "Data is " << data; } }However the challenge is :
QString* data = (QString*) ptr;How do it achieve the void* to QString conversion ?
@derric said in void* to QString C++:
QString* data = (QString*) ptr; qDebug() << "Data is " << data;As @eyllanesc has said: since
datais aQString *, if you output just that you get a pointer value, like0x65fe9c. If you want to see what it points to, you needqDebug() << "Data is " << *data;, which dereferences the pointer and will print theQString, which isHello Qthere.There is nothing special in this context because you chose to pass the (address of the) original
QStringas avoid *here.As I'm sure you are aware, if you use that C-style cast
(QString*) ptrand theptrpassed in does not point to aQString, disaster will occur when you go*data! :) -
@eyllanesc Thanks a ton
-
@derric said in void* to QString C++:
QString* data = (QString*) ptr; qDebug() << "Data is " << data;As @eyllanesc has said: since
datais aQString *, if you output just that you get a pointer value, like0x65fe9c. If you want to see what it points to, you needqDebug() << "Data is " << *data;, which dereferences the pointer and will print theQString, which isHello Qthere.There is nothing special in this context because you chose to pass the (address of the) original
QStringas avoid *here.As I'm sure you are aware, if you use that C-style cast
(QString*) ptrand theptrpassed in does not point to aQString, disaster will occur when you go*data! :)