2017-04-04 211 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; 
}; 

我試圖在GameWindow類,其中ResolutionSetting類被定義爲一個private類中GameWindow內初始化公共ResolutionSetting對象。初始化類的靜態常量成員,其中成員是私有類型?

我看到this交詳細介紹瞭如何初始化類staticconst構件,但是這不包括的情況下,當所述部件的類型定義爲從外部類範圍(被指定的類型,當諸如不可訪問與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