[Qt-interest] QSharedDataPointer and forward declared classes
Lukas Geyer
lgeyer at gmx.at
Mon Mar 1 11:25:39 CET 2010
Hello,
i'm currently reworking a class to be implicitly shared and I stumbled
upon a paragraph in the QSharedDataPointer documentation which
recommends using forward declaration for the corresponding QSharedData
subclass: "However, including the private subclass of QSharedData in the
same file as the public class containing the QSharedDataPointer is not
typical. ... Instead, we would just predeclare the private subclass
EmployeeData in employee.h this way: class EmployeeData;"
Taking a quick look at one of Qt's (4.6.1) implicitly shared classes
like QFont shows:
// qfont.h
class QFontPrivate;
class Q_GUI_EXPORT QFont
{
...
private:
friend class QFontPrivate;
QExplicitlySharedDataPointer<QFontPrivate> d;
}
Why does this work at all? IMHO classes used as template parameters have
to be fully declared, i.e. not beeing foward declared !?
Implementing a small test class
#include <QtCore/QExplicitlySharedDataPointer>
class TestClassData;
class TestClass
{
private:
QExplicitlySharedDataPointer<TestClassData> data;
};
show the expected result in form of the compiler (gcc-4.4.1) complaing
about invalid use of an incomplete type:
In file included from .../QExplicitlySharedDataPointer:1,
from testclass.h:4,
from main.cpp:1:
.../qshareddata.h: In destructor
‘QExplicitlySharedDataPointer<T>::~QExplicitlySharedDataPointer() [with
T = TestClassData]’:
testclass.h:9: instantiated from here
.../qshareddata.h:159: error: invalid use of incomplete type ‘struct
TestClassData’
testclass.h:6: error: forward declaration of ‘struct TestClassData’
.../qshareddata.h:159: warning: possible problem detected in invocation
of delete operator:
.../qshareddata.h:159: warning: invalid use of incomplete type ‘struct
TestClassData’
testclass.h:6: warning: forward declaration of ‘struct TestClassData’
.../qshareddata.h:159: note: neither the destructor nor the
class-specific operator delete will be called, even if they are declared
when the class is defined.
So, why does this work with Qt's built-in classes and how can i take
advantage of foward declared template parameters in my own classes?
Thanks in advance.
Best,
Lukas.
More information about the Qt-interest-old
mailing list