Implicit vs Explicit Sharing
Implicit sharing might not be right for the Employee class. Consider a simple example that creates two instances of the implicitly shared Employee class.
#include "employee.h"
int main()
{
Employee e1(1001, "Albrecht Durer");
Employee e2 = e1;
e1.setName("Hans Holbein");
}
After the second employee e2 is created and e1 is assigned to it, both e1 and e2 refer to Albrecht Durer, employee 1001. Both Employee objects point to the same instance of EmployeeData, which has reference count 2. Then e1.setName("Hans Holbein") is called to change the employee name, but because the reference count is greater than 1, a copy on write is performed before the name is changed. Now e1 and e2 point to different EmployeeData objects. They have different names, but both have ID 1001, which is probably not what you want. You can, of course, just continue with e1.setId(1002), if you really mean to create a second, unique employee, but if you only want to change the employee's name everywhere, consider using explicit sharing in the Employee class instead of implicit sharing.
If you declare the d pointer in the Employee class to be QExplicitlySharedDataPointer<EmployeeData>, then explicit sharing is used and copy on write operations are not performed automatically (i.e. detach() is not called in non-const functions). In that case, after e1.setName("Hans Holbein"), the employee's name has been changed, but both e1 and e2 still refer to the same instance of EmployeeData, so there is only one employee with ID 1001.
In the member function documentation, d pointer always refers to the internal pointer to the shared data object.
See also QSharedData and QExplicitlySharedDataPointer.
Member Function Documentation
QSharedDataPointer::QSharedDataPointer ()
Constructs a QSharedDataPointer initialized with a null d pointer.
QSharedDataPointer::QSharedDataPointer ( T * sharedData )
Constructs a QSharedDataPointer with d pointer set to sharedData and increments sharedData's reference count.
QSharedDataPointer::QSharedDataPointer ( const QSharedDataPointer<T> & other )
Sets the d pointer of this to the d pointer in other and increments the reference count of the shared data object.
QSharedDataPointer::~QSharedDataPointer ()
Decrements the reference count of the shared data object. If the reference count becomes 0, the shared data object is deleted. This is then destroyed.
const T * QSharedDataPointer::constData () const
Returns a const pointer to the shared data object. This function does not call detach().
See also data().
T * QSharedDataPointer::data ()
Returns a pointer to the shared data object. This function calls detach().
See also constData().
const T * QSharedDataPointer::data () const
This is an overloaded member function, provided for convenience.
Returns a pointer to the shared data object. This function does not call detach().
void QSharedDataPointer::detach ()
If the shared data object's reference count is greater than 1, this function creates a deep copy of the shared data object and sets the d pointer of this to the copy.
This function is called automatically by non-const member functions of QSharedDataPointer if copy on write is required. You don't need to call it yourself.
QSharedDataPointer::operator T * ()
Returns a pointer to the shared data object. This function calls detach().
See also data() and constData().
QSharedDataPointer::operator const T * () const
Returns a pointer to the shared data object. This function does not call detach().
bool QSharedDataPointer::operator! () const
Returns true if the d pointer of this is null.
bool QSharedDataPointer::operator!= ( const QSharedDataPointer<T> & other ) const
Returns true if other and this do not have the same d pointer. This function does not call detach().
T & QSharedDataPointer::operator* ()
Provides access to the shared data object's members. This function calls detach().
const T & QSharedDataPointer::operator* () const
This is an overloaded member function, provided for convenience.
Provides const access to the shared data object's members. This function does not call detach().
T * QSharedDataPointer::operator-> ()
Provides access to the shared data object's members. This function calls detach().
const T * QSharedDataPointer::operator-> () const
This is an overloaded member function, provided for convenience.
Provides const access to the shared data object's members. This function does not call detach().
QSharedDataPointer<T> & QSharedDataPointer::operator= ( const QSharedDataPointer<T> & other )
Sets the d pointer of this to the d pointer of other and increments the reference count of the shared data object. The reference count of the old shared data object of this is decremented. If the reference count of the old shared data object becomes 0, the old shared data object is deleted.
QSharedDataPointer & QSharedDataPointer::operator= ( T * sharedData )
This is an overloaded member function, provided for convenience.
Sets the d pointer og this to sharedData and increments sharedData's reference count. The reference count of the old shared data object of this is decremented. If the reference count of the old shared data object becomes 0, the old shared data object is deleted.
bool QSharedDataPointer::operator== ( const QSharedDataPointer<T> & other ) const
Returns true if other and this have the same d pointer. This function does not call detach().