Hi!
Thank you for your help!

Originally Posted by
Lykurg
If you create it on the heap, then you have to care about. But since they are implicit shared, simple create them on the stack and all is fine:
I am also a newbie in C++ so I want to make sure I correctly understood what you said.
(I am however familiar with C, Java and assembly language and I am trying to adjust to C++ and Qt using the concepts I learned in these other languages).
OK, from what I've understood creating it on the heap would mean using a "new" like so:
QString test = new QString("This is a test");
To copy to clipboard, switch view to plain text mode
Creating it on the stack would be like this:
QString test = "This is a test";
To copy to clipboard, switch view to plain text mode
But then I have also seen this, what does this do?:
QString test = QString("This is a test");
To copy to clipboard, switch view to plain text mode
(Since there is no new I would assume it does the same thing as the previous one and some people seem to prefer declaring it that way.)
The method I am calling looks something like this (I've simplified it from what it actually is):
// Do some substitution..
// Split at the # and trim what's between them
for (it = sl.begin(); it != sl.end(); ++it)
{
(*it) = (*it).trimmed();
}
// rejoin everything and put it back in the original string
str = sl.join(" ");
// process the string further once it's merged back
// (it's not exactly like that but it does give an idea
// of the kind of further processing done here... Yep, mapping
// the @ back to the separator used above is done on purpose...)
return text;
}
QString makeTest(QString str)
// Do some substitution..
str.replace("*", QString("!"));
// Split at the # and trim what's between them
QStringList sl = str.split('#');
QStringList::iterator it;
for (it = sl.begin(); it != sl.end(); ++it)
{
(*it) = (*it).trimmed();
}
// rejoin everything and put it back in the original string
str = sl.join(" ");
// process the string further once it's merged back
// (it's not exactly like that but it does give an idea
// of the kind of further processing done here... Yep, mapping
// the @ back to the separator used above is done on purpose...)
str.replace(QString("@"), QString("#"));
return text;
}
To copy to clipboard, switch view to plain text mode
(BTW, from what I have read QStringList is also implicitly shared,)
I don't want to pass the QString by reference as I don't want the processing I'm doing to interfere with the rest of the treatment.
If I understood your reply correctly the method I have shown above (probably) only uses the stack so it should free all of the memory it uses when it terminates, right?? What about the return parameter, is it freed when the calling method terminates?
There is something about implicit sharing I'm not sure I understand though... Should it be able to free the memory allocated even if I had done a "new QString" since it apparently maintains a count of how many referrences there is to the object ?
Thank you very much for your help!
Nick
Bookmarks