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;
};

I am attempting to initialize the public ResolutionSetting objects within the GameWindow class, where the ResolutionSetting class is defined to be a private class within GameWindow.

I've seen this post which details how to initialize static const members of classes, however this doesn't include the case when the type definition of said member is inaccessible from outside class scope (such as when the type is specified with private access rules).

And how do I go about initializing these objects? Would:

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

or

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

suffice? (assuming I can correct any possible issues with ResolutionSetting being inaccessible).

1 Answer 1

1

suffice? (assuming I can correct any possible issues with ResolutionSetting being inaccessible).

Yes suffice. GameWindow::ResolutionSetting is accessible in the initializer expression of the static member.

So you can define them as:

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

From the standard, $12.2.3.2/2 Static data members [class.static.data]:

The initializer expression in the definition of a static data member is in the scope of its class ([basic.scope.class]).

Minimal sample

Not the answer you're looking for? Browse other questions tagged or ask your own question.