QSharedPointer, how do I pass them, and do I need them?

I tried to understand the general pointer for several days, and it seems that I can not get it. Not sure if this is just obvious or if it's too complicated. First of all, can someone please give me an example in which you would STRONGLY use common pointers. The Wikipedia examples don't make any sense to me. And how would you pass a shared pointer to another function or create an object with a shared pointer. So how do you go about it and where do you use it? Any information or examples would be great.

In addition, I have this problem when I do not know what to use. I have this function where I QFile and pass it to a function in another class. This function takes the file as a QIODevice* , and then creates an object containing the file. I was wondering what the best solution would be here and how (if I should) use a generic pointer here? How can I make a generic pointer with <QFile> and pass it where the function accepts <QIODevice> . It looks like I'm not getting generic pointers at all ...

My other approach would be to place the QFile selection in a QScopedPointer . Then I pass it to the class and when creating the object in which the file will be stored, I use QPointer or QScopedPointer . At the end of the first calling function, should I call take () right?

 function () { QScopedPointer<QFile> item(new QFile("filename")); SomeClassObject->doStuff(item.data()); item.take(); } --------------------------------- SomeClass::doStuff(QIODevice *item) { _currentObject = new MyObject(item); // should _currentObject be a smartpointer? ... } --------------------------------- class MyObject { QPointer<QIODevice> _item; ... MyObject(QIODevice *item) { _item = item; } } 

So, I want to store pointers and how to handle them at creation time if the β€œnew” throws an exception.

+4
source share
1 answer

The point of shared pointers (and other similar wrappers for pointers) is intended to properly control the destruction of a pointer to an object. This is instead of manually verifying that you are deleting the last copy (and only the last copy), the generic pointer will take care of this for you when it goes beyond. The joint part means that you can create copies of this wrapper object (a shared pointer object), and it will β€œsplit” the pointer to the object between the copies (as if you made a copy of a regular pointer) with the added benefit of the above.

As for your code, SomeClass::doStuff() should have a QScopedPointer<QFile> parameter (instead of QIODevice* one), since you are passing it an item that has this type.

Same thing with the MyObject constructor: does it have a parameter of type QPointer<QIODevice> or QSharedPointer<QIODevice> . In general, wherever you used pointers, use QSharedPointer instead (with the appropriate template type). This will save you from the headaches associated with subsequent access to remote objects.

Of course, sometimes you really need a raw QIODevice pointer (for example, to call a third-party library), then you must use the member function data() of the shared pointer object. Just make sure that you do not save (i.e. storage or otherwise copy beyond what is needed) the returned raw pointer, because it undermines the purpose of shared pointers - shared pointers will not know about your extra raw pointer, which is not under control common pointer objects.

EDIT: take() releases ownership of an object with a pointer to a pointer with scope, so when a pointer with scope is destroyed, it does not delete the object. You could use ant in a situation where you transferred ownership of something else - as in your case to MyObject .

+8
source

Source: https://habr.com/ru/post/1403008/


All Articles