Re: Converting C++ to Qt4
Did you take a look at QSharedDataPointer?
Also, nothing stops you from using boost::shared_ptr together with Qt...
Re: Converting C++ to Qt4
Thank you. So one question. I think from reading the doc on it. It will automatically allow a end-user to do like Value* v = new Value(22); and not have to worry about deleting it?
Re: Converting C++ to Qt4
no, something like that is just about impossible in C++.
The way I read it, it works like boost::shared_ptr.
QSharedDataPointer<Value> v = new Value(22);
You have to assign the 'naked' pointer to a QSharedDataPointer. Once the (last copy of this) QSharedDataPointer goes out of scope the reference count gets zero and the object deleted.
(Note that this is a huge difference to boost::shared_ptr: a QSharedDataPointer "detaches" when you call a non-const op on it (effectifely creating a separate copy).
This is what you want for a value-like class like QString (see below). It might not be at all what you want.)
If you do not want to implement a shared value class, stick to boost::shared_ptr, imo.
The other thing is, that this class was designed for implementing 'value-like' shared objects. Like a QString.
You will hardly ever write
QString hides this and maybe uses a QSharedDataPointer internally.
The docs illustrated how to do this.
(This is basically a more adavanced form of the pimpl idiom with better support for efficient (and correct) copying of objects.)
HTH
Re: Converting C++ to Qt4
QSharedDataPointer operates on QSharedData, so this is probably not going to work.
Something like:
Code:
SomeTypeVeryWisePointer ptr = new SomeType;
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:
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:
Code:
SharedPtr ptr1 = new SomeType;
SharedPtr ptr2 = ptr1;
delete ptr1; // this is just an example...
ptr2->stillWorking();
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:
Code:
SomeType obj1;
SomeType obj2 = obj1;
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.
Code:
template <class T> class GuardedPtr {
public:
GuardedPtr(T *o) { ptr = o; }
~GuardedPtr(){ delete ptr; }
private:
T *ptr;
};
and then use it like this:
Code:
xyz *ptr = new xyz;
GuardedPtr<ptr> guard(ptr);
//...
// ptr gets deleted when guard goes out of scope
Just be aware it's a stupid class that may cause much trouble.
Re: Converting C++ to Qt4
I think I finally got it working. But I thought you would call detach before setting the value and not after?
Code:
#include <QExplicitlySharedDataPointer>
#include <QSharedData>
#include <QDebug>
public:
ValueData()
: m_value(1) {
}
ValueData(int value)
: m_value(value) {
}
~ValueData() { }
ValueData(const ValueData& v)
m_value(v.m_value) {
}
void print() {
qDebug() << m_value;
}
int m_value;
};
class ValueT {
public:
ValueT() {
d = new ValueData;
}
ValueT(int value) {
d = new ValueData(value);
}
void setValue(int value) {
d->m_value = value;
d.detach();
}
void print() {
d->print();
}
private:
QExplicitlySharedDataPointer<ValueData> d;
};
But one last question. What happens if the end user does
Code:
ValueT* v = new ValueT(123);
Will it cause a a memory leak if s/he doesn't call delete?
Re: Converting C++ to Qt4
Yes, then you must delete it yourself
Re: Converting C++ to Qt4
It doesn't like when you create your own operator= or copy constructor. Because I tried to make like 3 of them and when I did. It would never print to console unless I removed them :(
Re: Converting C++ to Qt4
Quote:
Originally Posted by
ComaWhite
I think I finally got it working. But I thought you would call detach before setting the value and not after?
With an explicitely shared pointer you should call detach only if you want to separate the two objects (so copy is always a shallow copy by default). If you want to always separate them, use QSharedDataPointer which calls detach itself (copy on write).