Hi all,
Just tried to create my first data class using Qt's implicit sharing, but the documentation out there is misleading in multiple cases.
My header currently looks like this:
class MyData;
class MyBase
{
public:
MyBase(const QString& name, const QString& address);
const QString& getName() const;
const QString& getAddress() const;
private:
QSharedDataPointer<MyData> d;
};
{
public:
};
class MyData;
class MyBase
{
public:
MyBase(const QString& name, const QString& address);
const QString& getName() const;
const QString& getAddress() const;
private:
QSharedDataPointer<MyData> d;
};
class MyData : public QSharedData
{
public:
QString name;
QString address;
};
To copy to clipboard, switch view to plain text mode
Source file:
MyBase::MyBase(const QString& name, const QString& address)
{
d = new MyData();
d->name = name;
d->address = address;
}
const QString& getName() const
{
return d->name;
}
const QString& getAddress() const
{
return d->address;
}
MyBase::MyBase(const QString& name, const QString& address)
{
d = new MyData();
d->name = name;
d->address = address;
}
const QString& getName() const
{
return d->name;
}
const QString& getAddress() const
{
return d->address;
}
To copy to clipboard, switch view to plain text mode
This already compiles, but misses some parts depending on what documentation you read. What comes to mind:- Empty destructor ~MyBase() {}
- Copy constructor MyBase(const MyBase& other) : d(other.d) {}
- Assignment operator operator=(const MyBase& other) {if(this != &other) d = other.d; return *this;}
- Constructors, destructors and assignment operators in MyData as well
Is any of them strictly required or does my implementation also work well without them? Maybe some are implicitly compiler-generated, I have no idea.
I've read that this also depends on whether you declare MyData inside the MyBase header file. I definitely want to do this, so please tell me if this is the reason I can leave out these things.
Thank you!
Bookmarks