// 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
类中的公共GameWindow
对象,其中ResolutionSetting
类被定义为private
类GameWindow
类static
我看过this帖子详细说明了如何初始化const
private
个成员,但是这并不包括所说的类型定义的情况成员无法从外部类范围访问(例如,使用const GameWindow::ResolutionSetting GameWindow::w640h480(640, 480);
访问规则指定类型时)。
我该如何初始化这些对象?将:
const GameWindow::ResolutionSetting GameWindow::w640h480 = GameWindow::ResolutionSetting(640, 480);
或
ResolutionSetting
足够? (假设我可以纠正const Eigen::Matrix<double, 100, 1> v = Eigen::Matrix<double, 100, 1>::Random();
const int num_rows = 10;
const int num_cols = 1;
const int starting_row = 0;
const int starting_col = 0;
const double sum_of_first_ten = v.block(starting_row, starting_col, num_rows, num_cols).sum();
const double mean_of_first_ten = sum_of_first_ten / num_rows;
无法访问的任何可能问题。)
答案 0 :(得分: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])。