【问题标题】:quotes after the class name but before open brackets [duplicate]在类名之后但在左括号之前的引号[重复]
【发布时间】:2016-05-11 01:24:26
【问题描述】:

我有一个简单的问题,我想不通。这段代码不是我写的。我知道 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() }
{

【问题讨论】:

  • 这不是类定义。它是一个构造函数。它正在构造成员变量。

标签: c++ class instance


【解决方案1】:

这只是一个构造函数。

Game::Game 构造函数正在构造该类的两个成员,tetromino_moveTime_。大括号(不是方括号)是 uniform initialization syntax,它是 C++11/C++14 的新内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-05
    • 2014-08-30
    • 2022-01-06
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    相关资源
    最近更新 更多