LoginSignup
6
5

More than 5 years have passed since last update.

unique_ptrでデストラクタをprivateに持つクラスを使う

Posted at

下記のようなクラスを書いてコンパイルすると怒られます。

class GameUser final
{
public:

    GameUser(const GameUser&) = delete;
    GameUser& operator=(const GameUser&) = delete;

    static GameUser* getInstance();

private:

    GameUser() = default;
    ~GameUser() = default;

    static std::unique_ptr<GameUser> _instance;
};
Calling a private destructor of class 'GameUser'

GameUserクラスのデストラクタがプライベートで呼べない(´・ω・)

対応

std::unique_ptr<>::deleter_typefriendにする。

class GameUser final
{
    // 省略

    friend std::unique_ptr<GameUser>::deleter_type;
    // または
    friend decltype(_instance)::deleter_type;
};
6
5
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
6
5