13,005
回編集
397行目: | 397行目: | ||
return 0; | return 0; | ||
} | |||
</syntaxhighlight> | |||
<br> | |||
==== Q_DECLARE_PRIVATE / Q_DECLARE_PUBLIC ==== | |||
これは、Qtのプライベート実装パターンと共有データの管理に関連している。<br> | |||
<br> | |||
Qtのd-pointerイディオムを実装するために使用する。<br> | |||
このパターンは、クラスの実装詳細を隠蔽して、バイナリ互換性を維持するのに役立つ。<br> | |||
<br> | |||
また、プライベートクラスとパブリッククラス間で相互にアクセスが可能となる。<br> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
// MyClass.hファイル | |||
#include "MyClassPrivate.h" | |||
class MyClassPrivate; | |||
class MyClass | |||
{ | |||
private: | |||
Q_DECLARE_PRIVATE(MyClass) | |||
MyClassPrivate *d_ptr; | |||
public: | |||
MyClass() : d_ptr(new MyClassPrivate(this)) | |||
{} | |||
~MyClass() | |||
{ | |||
delete d_ptr; | |||
} | |||
void doSomething() | |||
{ | |||
Q_D(MyClass); | |||
d->internalFunction(); | |||
} | |||
}; | |||
</syntaxhighlight> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
// MyClassPrivate.hファイル | |||
class MyClassPrivate | |||
{ | |||
public: | |||
MyClassPrivate(MyClass *q) : q_ptr(q) {} | |||
void MyClassPrivate::internalFunction() | |||
{ | |||
Q_Q(MyClass); | |||
// publicクラスのメンバーにアクセスできる | |||
someData = 42; | |||
} | |||
MyClass *q_ptr; | |||
Q_DECLARE_PUBLIC(MyClass) | |||
int someData; | |||
}; | |||
</syntaxhighlight> | |||
<br> | |||
==== Q_DECLARE_SHARED ==== | |||
暗黙的な共有 (implicit sharing) を使用するクラスを宣言するために使用する。<br> | |||
これにより、クラスのインスタンスを効率的にコピーできる。<br> | |||
<br> | |||
これは、メモリ使用量を削減でき、大きなオブジェクトの不必要なコピーを避けることができる。<br> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
// MySharedData.hファイル | |||
#include <QSharedData> | |||
#include <QSharedDataPointer> | |||
#include "MySharedDataPrivate.h" | |||
class MySharedDataPrivate; | |||
class MySharedData | |||
{ | |||
private: | |||
QSharedDataPointer<MySharedDataPrivate> d; | |||
public: | |||
MySharedData() : d(new MySharedDataPrivate) {} | |||
MySharedData::MySharedData(const MySharedData &other) : d(other.d) {} | |||
MySharedData &operator=(const MySharedData &other) | |||
{ | |||
if (this != &other) | |||
d = other.d; | |||
return *this; | |||
} | |||
~MySharedData(){} | |||
void setValue(int value) | |||
{ | |||
d->value = value; | |||
} | |||
int MySharedData::value() const | |||
{ | |||
return d->value; | |||
} | |||
}; | |||
// クラスの宣言に関連する情報を提供して、 | |||
// そのクラスを使用するコードがこの情報にアクセスする必要があるため、ここで宣言する | |||
Q_DECLARE_SHARED(MySharedData) | |||
</syntaxhighlight> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
// MySharedDataPrivate.hファイル | |||
class MySharedDataPrivate : public QSharedData | |||
{ | |||
public: | |||
MySharedDataPrivate() : value(0) {} | |||
MySharedDataPrivate(const MySharedDataPrivate &other) : QSharedData(other), value(other.value) {} | |||
int value; | |||
}; | |||
</syntaxhighlight> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
// 使用例 | |||
void useSharedData() | |||
{ | |||
MySharedData data1; | |||
data1.setValue(42); | |||
MySharedData data2 = data1; // データは共有される | |||
data2.setValue(100); // この時点で、データのコピーが作成される | |||
qDebug() << data1.value(); // 出力: 42 | |||
qDebug() << data2.value(); // 出力: 100 | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |