Reputation: 1123
I have 2 classes defined in the .h file of my app
class GroupElement : public QSharedData
{
public:
int Id;
int GroupIndex;
QString Title;
QSharedDataPointer<AccessDescriptor> pAccessDescriptor;
QString Owner;
};
class Class2 : public GroupElement
{
public:
QSharedDataPointer<QList<int> > Categories;
// ... many other class members
}
class3 : public QObject
{
Q_OBJECT
public:
template <class T> QSharedDataPointer<QList<T> > toTQList(QVariant qv );
//...more ....
}
In another function I try to assign a value to 'Categories'
QSharedDataPointer<Class2>
Class3::afunction(QVariantMap o)
{
QSharedDataPointer<Class2> getResult(new Class2());
getResult-> Categories = toTQList<int>(o["Categories"]);
// ... more code ...
}
When compiling the code as shown, the compiler complains "class ...has no member named 'ref'". This usually indicates that I need to add public QSharedData as the base class. But in this case Class2 is already derived from GroupElement which has QSharedData as a base class. If I add QSharedData as a base class to Class 2, I get warning messages "direct base 'QSharedData' inaccessible in 'Class2' do to ambiguity. I think may have run into one of the 'diamond problem' with multiple inheritance but I'm not experienced enough in Qt or c++ to know how to resolve it.
Help would be greatly appreciated.
Upvotes: 0
Views: 789
Reputation: 5718
QSharedDataPointer
has a very specific use-case, which is where you want to share data between instances, with copy-on-write behaviour - that is, as soon as one instance, tries to modify it, it detaches and it now has it's own separate copy. For this to work the objects contained in a QSharedDataPointer
must inherit from QSharedData
, which provides the reference counting required for this to work.
So your difficulty is nothing to do with the diamond pattern, you simply cannot store a QList
directly in a QSharedDataPointer
. It's also not at all clear from your example that this is what you really want to do. What are you actually trying to achieve with your design? It may well be that a QSharedPointer
is what you really want.
I would recommend you read https://www.qt.io/blog/2009/08/25/count-with-me-how-many-smart-pointer-classes-does-qt-have for an explanation of Qt's smart pointer classes.
Upvotes: 1