0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

モダンなシングルトン?(C++ 11以降)

Posted at

#初めに
この記事では、C++11以降の機能を使ったモダンなシングルトンの実装方法を記載していきます。

#シングルトン
・シングルトンとは
「ゲーム中1つしか存在しないインスタンス」を作るデザインパターン
・目的
設定管理や初期化など、「1つだけで十分」な機能に使う
例:ゲームの初期設定、ログ管理、リソース管理
今回は、ゲームの初期設定用クラスにシングルトンを使用しました。

#実装内容
image.png

#ポイント
・スレッドセーフ
staticローカル変数はC++11以降、自動でスレッドセーフ。
マルチスレッド対応も安全。
・初期化順序問題を回避
グローバルオブジェクトと違い、はじめてつかわれたときに確実に初期化できる。
・簡潔で明瞭
コード量が少なく、意図がはっきりしている。
・禁止を明示
= deleteによって、コピーや代入を明示的に禁止できる。

#最後に
以上が私が学んだモダンなシングルトンについてでした。
後々もっと良い方法があれば、その都度更新したいと思います。

0
0
4

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?