LoginSignup
0
0

More than 5 years have passed since last update.

Effective C++ (3rd) > 31項 > オブジェクトの実装をポインタの後ろに隠す > クラスをインタフェース用のものと実装用のものに分ける

Last updated at Posted at 2015-05-15

引用: Effective C++ 第3版

31項 ファイル間のコンパイル依存性をなるべき小さくしよう
...

「オブジェクトの実装をポインタの後ろに隠す」ということを自分ですることができます。そのための一つの方法は、クラスをインタフェース用のものと実装用のものに分けるというものです。
たとえば、...実装用のクラスPersonImplとインタフェース用のクラスPerson...

#include <string>
#include <memory>
class PersonImpl; // Personを実装するクラスの前方宣言
class Date;       // Personで使うクラスの前方宣言
class Address;    // Personで使うクラスの前方宣言

class Person {
public:
  Person(const std::string& name, const Date& birthday,
     const Address& addr);
  std::string name() const;
  std::string birthDate() const;
  std::string address() const;
  ...
private:
  std::tr1::shared_ptr<PersonImpl> pImpl; // 実装へのポインタ
};

...
このようにデザインすると、Personを利用するコードは、DateやAddress、Personの実装の詳細から切り離されることになります。そのため、これらのクラスの実装は自由に変えることができるのです。それらの実装を変更しても、Personを利用するコードを再コンパイルする必要はありません。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0