[Qt-interest] QSharedDataPointer and forward declared classes

Thiago Macieira thiago at kde.org
Tue Mar 2 10:05:57 CET 2010


Em Terça-feira 02 Março 2010, às 09:04:00, Lukas Geyer escreveu:
> The compiler error was caused by an missing ctor/dtor for TestClass
> (d'oh!).
> 
> > #include <QtCore/QExplicitlySharedDataPointer>
> > class TestClassData;
> > class TestClass
> > {
> > 
> > private:
> >    QExplicitlySharedDataPointer<TestClassData> data;
> > 
> > };
> 
> TestClass() and ~TestClass() missing here.

Actually, there are two more missing.

Any C++ developer should know the answer to this: what are the four implicit 
methods in a class? (i.e. methods that the compiler will create for you if you 
don't say otherwise)

So you actually need to define all four in your class and they must not be 
inline:

class TestClass
{
public:
    TestClass();
    TestClass(const TestClass &other);
    ~TestClass();
    TestClass &operator=(const TestClass &other);

private:
    QExplicitlySharedDataPointer<TestClassData> data;
};

If they are inline, the compiler will instantiate the access to TestClassData 
in the caller's code, which means TestClassData needs to be fully defined and 
that's not what you want. Note that implicit implies inline.

If you don't want your object to be copyable, then move the copy constructor 
and assignment operators to the private: part. You don't have to implement 
them, because no one will be calling them. That's what the Q_DISABLE_COPY 
macro does, until C++0x comes with deleted functions.

Bonus: you may want to add operator== and operator!= too.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Senior Product Manager - Nokia, Qt Development Frameworks
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100302/f22ba6b0/attachment.bin 


More information about the Qt-interest-old mailing list