LoginSignup
10
8

More than 5 years have passed since last update.

pimplなんてクソクラエ

Posted at

もともとpimplイディオムは個人的に「インターフェイスと中身の拘束が言語ではなくコードに依る(=誤りの元)」や「無駄にメモリ確保が必要」などという理由で嫌っていたが、C++では以下のようにすることでpimplイディオムは完全に不要になる。

hoge.h
class Hoge{
  ...
  static Hoge* New();
  virtual void Huga();
  ...
};
hoge.cc
#include "hoge.h"
class Muga : pulic Hoge{
  void Huga() final{
    ...
  };
};

Hoge* Hoge::New(){
  return new Muga;
}

多態する必要があったりファクトリが必要だったりするのが面倒なのと、実装をさらに継承するにはソースの修正が必要なのが難点だが、ともかくファクトリ中で実装を選択するようにすると多態の実装をまるごと隠蔽できる。

10
8
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
10
8