[Qt-interest] QSharedDataPointer and forward declared classes

Raul Metsma raul at innovaatik.ee
Mon Mar 1 12:27:44 CET 2010


I figured out how to fix this

you have to declare in your header

Object( const Object &other );
~Object();
Object&	operator=( const Object &other );

and in cpp

Object( const QObject &other ): d( other.d ) {}
Object::~Object() {}
Object& Object::operator=( const Object &other ) { d = other.d; return 
*this; }

if you define destructor {} in header it will fail

Raul Metsma

On 03/01/2010 12:25 PM, Lukas Geyer wrote:
> 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.
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest



More information about the Qt-interest-old mailing list