Implicit Sharing
-
Hi friends, I have read that implicit sharing is a clever methodology in saving resources. In that case, Can any one please explain how I can verify two QString objects in which one is allocated with some data and the other one is assigned with the former one.
Say
QString a="aaa"
QString b=a;//shallow copy
qDebug()<<a.toStdString().c_str();//0x100
qDebug()<<b.toStdString().c_str();//0x101
Why do I get two different pointer addresses if shallow copy was being done? -
Hi,
Because toStdString returns a std::string and c_str() returns the address of the first char from that std::string object.
Note that calling it the way you do, will also get you different values for different calls because of the lifetime of the objects involved.
In any case, the pointer you are getting has just no relation with the original QString objects.
-
@prinzkm said in Implicit Sharing:
So it is a bad practice to use toStdString() over and over for the same variable.Isn't it?
Yes. You can assign what toStdString() returns to a variable instead of calling it over and over.