QString issue
-
welcome to devnet
"Well, what is still open with this guideline?":http://doc.qt.io/qt-5/qstring.html#details
@
#include <QString>
#include <QDebug>void otherFoo ( const QString & refStr )
{
qDebug() << refStr;
}void foo ()
{
QString str = "Hello";
otherFoo ( str );
}
@Note: just typed, not tested.
-
I'm just getting back into c++ after a number of years but in general you have to be careful of scope.
if you have a function and you use a QString in that function it might go out of scope:
@
void somefunction()
{
QString myString( "Hello World" );
}
@myString only lives while in somefunction.
But in my past you can pass things by reference so you could so something like:
@
void funca()
{
QString myString("Hello World");funcb( &myString );
}void funcb( QString *stg )
{
stg-> ... do whatever ...
}
@You can also new up your own version and keep it around until you want to delete it.
[edit: added missing coding tags @ SGaist]
-
There are almost no usecases where you would want to use a pointer or new with QString.
If you want to pass it into a function do it by reference (like koahnig suggested).
If you need a copy somewhere don't hesitate either. QString, like many other Qt types uses PIMPL idiom and copy on write, so it's pretty cheap to pass aound.
Naked pointers lead do leaks and maintanace problems. This is true for simple types like QString especially. -
I admit to being out of touch with c++ for years and also being new to Qt.
Having not studied QString I admit to naivety. However having worked for so long with languages where you as the coder must keep track of things I'm used to being very very aware of pointers and references.
I understand new paradigms offer greater protection but frankly a pointer is a pointer is a pointer. If you know what you are doing memory leaks don't happen.
But I will listen and learn. Thanks for the input!
-
bq. I’m used to being very very aware of pointers and references.
That's great. I'm all for it. If anything it makes you a better coder. Qt is a great library that keeps a lot of that stuff from you, but knowing how an engine works makes you a better driver even if you don't plan on building a car yourself :) You can also see a kind of new wave in c++ where naked pointers are becoming more and more faux pas, with light smart pointers and "RAII":http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization as the preferable way of resource management.
Anyway, a QString is really a pointer to internal data structure that has some storage, so having a pointer to it is almost like char*** - a level of indirection that you neither want nor need (most of the time). -
Hi,
To add to Chris' notes, Qt has numerous "value" classes that are intended to be used this way -- allocated on the stack, not the heap. See the article on "Implicit Sharing":http://doc.qt.io/qt-5/implicit-sharing.html for more info.
-
In general the great advantage of C++ is references. Besides making the typing a bit easier it is the avoidance of memory leaks. Finally the const declaration of reference helps to avoid non-intended changes.
For someone switching from C to C++ that is not obvious and a real nuisance at start. However, after getting used you really appreciate those features. They save lots of debugging hours.
IMHO pointers shall be used when absolutely necessary, but not when they are also a way to do. -
Thanks all. I still have a lot to learn. It is great to have so many people here that are willing to set others straight on better ways to do things.
I do see that from my early C++ days things have definitely moved from straight pointers. So more reading, more work and hopefully I'll save myself hours of debugging and time!
All great info thanks!