2017-04-04 11 views
1
// gamewindow.hpp 

#include <SFML/Graphics.hpp> 
#include <string> 
#include <cstdint> 

class GameWindow : public sf::RenderWindow 
{ 
public: 
    GameWindow(const uint32_t&, const uint32_t&, const std::string&); 
    void toggleFullscreen(); 

    static const ResolutionSetting w640h480, 
            w1600h900, 
            w1920h1080, 
            w2560h1440; 
private: 
    class ResolutionSetting 
    { 
    public: 
     ResolutionSetting(const uint32_t& width, const uint32_t& height) 
     : res(width, height) {} 
    private: 
     sf::Vector2u res; 
    }; 

    std::string _title; 
    uint32_t _width; 
    uint32_t _height; 
}; 

私はResolutionSettingクラスがGameWindowprivateクラスとして定義されGameWindowクラス内のパブリックResolutionSettingオブジェクトを初期化しようとしています。私が言ったメンバーの型定義は、このようなタイプが指定されている場合など、外部のクラススコープ(からアクセスできないときしかし、これはケースが含まれていない、クラスのstaticconstメンバーを初期化する方法の詳細thisポストを見てきましたクラスのstatic constメンバーを初期化します。メンバーはプライベートタイプですか?

アクセスルールはprivate)。

これらのオブジェクトをどのように初期化するのですか?

const GameWindow::ResolutionSetting GameWindow::w640h480(640, 480); 

または

const GameWindow::ResolutionSetting GameWindow::w640h480 = GameWindow::ResolutionSetting(640, 480); 

で十分:でしょうか? (私がResolutionSettingにアクセスできないと考えられる問題を修正できると仮定して)。

答えて

1

で十分ですか? (私はResolutionSettingにアクセスできないと考えられる問題を修正できると仮定します)。

はい十分です。 GameWindow::ResolutionSettingは、静的メンバーの初期化式でアクセスできます。 、標準から

const GameWindow::ResolutionSetting GameWindow::w640h480(640, 480); 
const GameWindow::ResolutionSetting GameWindow::w1600h900{1600, 900}; 
const GameWindow::ResolutionSetting GameWindow::w1920h1080 = GameWindow::ResolutionSetting(1920, 1080); 

$12.2.3.2/2 Static data members [class.static.data]を::

だから、としてそれらを定義することができ

静的データメンバーの定義における初期化子式はそのクラスのスコープ内にあります([ basic.scope.class])。

Minimal sample

関連する問題