QSharedDataPointer operates on QSharedData, so this is probably not going to work.
Something like:
SomeTypeVeryWisePointer ptr = new SomeType;
SomeTypeVeryWisePointer ptr = new SomeType;
To copy to clipboard, switch view to plain text mode
doesn't make much sense if you want the data to be deleted when ptr goes out of scope. If you want that, simply write:
SomeType obj;
SomeType obj;
To copy to clipboard, switch view to plain text mode
When the variable goes out of scope, the compiler will delete it for you.
There is also a chance you want a reference counting pointer:
SharedPtr ptr1 = new SomeType;
SharedPtr ptr2 = ptr1;
delete ptr1; // this is just an example...
ptr2->stillWorking();
SharedPtr ptr1 = new SomeType;
SharedPtr ptr2 = ptr1;
delete ptr1; // this is just an example...
ptr2->stillWorking();
To copy to clipboard, switch view to plain text mode
In that case QSharedDataPointer (or rather QExplicitelySharedDataPointer) will be helpful if you implement SomeType as a shared class. Then you'll be able to write:
SomeType obj1;
SomeType obj2 = obj1;
SomeType obj1;
SomeType obj2 = obj1;
To copy to clipboard, switch view to plain text mode
And both facade objects will point to the same set of data.
You might also want a guarded pointer (similar to what QMutexLocker does). In that case you can implement it yourself.
template <class T> class GuardedPtr {
public:
GuardedPtr(T *o) { ptr = o; }
~GuardedPtr(){ delete ptr; }
private:
T *ptr;
};
template <class T> class GuardedPtr {
public:
GuardedPtr(T *o) { ptr = o; }
~GuardedPtr(){ delete ptr; }
private:
T *ptr;
};
To copy to clipboard, switch view to plain text mode
and then use it like this:
xyz *ptr = new xyz;
GuardedPtr<ptr> guard(ptr);
//...
// ptr gets deleted when guard goes out of scope
xyz *ptr = new xyz;
GuardedPtr<ptr> guard(ptr);
//...
// ptr gets deleted when guard goes out of scope
To copy to clipboard, switch view to plain text mode
Just be aware it's a stupid class that may cause much trouble.
Bookmarks