LoginSignup
0
0

More than 5 years have passed since last update.

c++11 > nullオブジェクトの挿入

Last updated at Posted at 2015-07-27

引用: ゲームプログラマのためのコーディング技術 by 大圖衛玄(Moriharu Ohzu)さん

nullオブジェクトとは、nullポインタの代わりとなるダミーオブジェクトのことです。

元々のコード
if (player != nullptr) {
  player->move();
}
...
if (player != nullptr) {
  player->draw();
}

nullオブジェクトクラスを作成する

class NullActor : public Actor {
   ...
   virtual void move() override { } // do nothing
   virtual void draw() override { } // do nothing
};
player = new NullActor();
変更後のコード
player->move();
player->draw();

if文がなくなったため、すっきりした。

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