在类名之后但在中括号之前加引号

quotes after the class name but before open brackets

本文关键字:加引号 之后      更新时间:2023-10-16

我有一个简单的问题想不通。我没有写这个代码。我知道tetromino_和moveTime_是两个不同类的实例。我的问题是,为什么它们在括号外声明,但仍然在类内。这个实例声明方法有名称吗?

Game::Game() :
tetromino_{ static_cast <Tetromino::Type>(rand() % 7) },
moveTime_{ SDL_GetTicks() }
{
    //srand(time(NULL));
    //initialize SDL and if it fails, present an error
    if (SDL_Init(SDL_INIT_VIDEO) != 0){
        throw std::runtime_error("_Init(SDL_INIT_VIDEO)");
    }
    //width of thhe window is 650/2 and the  height is 650
    SDL_CreateWindowAndRenderer(650 / 2, 650, SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS, &window_, &renderer_);
    //set the window position
    SDL_SetWindowPosition(window_, 365, 1);
}

如果我的问题不清楚,这叫什么

Game::Game() :
tetromino_{ static_cast <Tetromino::Type>(rand() % 7) },
moveTime_{ SDL_GetTicks() }
{

这只是一个构造函数。

Game::Game构造函数正在构造类的两个成员tetromino_moveTime_。大括号(而非括号)是统一的初始化语法,它是C++11/C++14的新语法。