简体   繁体   English

在构造函数初始化程序列表中使用大括号了解奇怪的语法

[英]Understanding the weird syntax with curly braces in a constructor initializer list

So I was just browsing the source code of a library when I encountered this. 因此,遇到此问题时,我只是在浏览库的源代码。

Font::Font(const sf::Font& font) :
        m_font{std::make_shared<sf::Font>(font)}
    {
    }

I don't understand the syntax 我不懂语法

m_font{..}

What is it? 它是什么? What does it do. 它有什么作用。 I am sorry if this is really stupid question. 如果这真是愚蠢的问题,我感到抱歉。 I don't know what to Google, so asking here. 我不知道谷歌该怎么做,所以在这里问。

This is described on cppreference , but in a somewhat hard to read format: 这是在cppreference上描述的,但格式有点难于理解:

The body of a function definition of any constructor, before the opening brace of the compound statement, may include the member initializer list , whose syntax is the colon character : , followed by the comma-separated list of one or more member-initializers , each of which has the following syntax 在复合语句的大括号之前,任何构造函数的函数定义的主体都可以包括成员初始化列表 ,其语法为冒号: ,然后是一个或多个成员初始化的逗号分隔列表,每个列表其中具有以下语法

... ...

class-or-identifier brace-init-list (2) (since C++11) 类或标识符大括号初始化列表 (2) (自C ++ 11起)

... ...

2) Initializes the base or member named by class-or-identifier using list-initialization (which becomes value-initialization if the list is empty and aggregate-initialization when initializing an aggregate) 2)使用列表初始化来初始化由类或标识符命名的基或成员(如果列表为空,则变为值初始化,而在初始化集合时,则为集合初始化)

What this is trying to say is that X::X(...) : some_member{some_expressions} { ... } causes the some_member class member to be initialised from some_expressions . 这是想说的是X::X(...) : some_member{some_expressions} { ... }导致some_member类成员从some_expressions初始化。 Given 给定

struct X {
    Y y;
    X() : y{3} {}
};

the data member y will be initialised the exact same way a local variable Y y{3}; 数据成员y将以与局部变量Y y{3};完全相同的方式初始化Y y{3}; would be initialised. 将被初始化。

In your case, std::make_shared<sf::Font>(font) produces the value that will be used to initialise the m_font class member. 在您的情况下, std::make_shared<sf::Font>(font)会生成将用于初始化m_font类成员的值。

That is a list initialization aka brace initializer list. 那是一个列表初始化又叫括号初始化列表。 More specifically, in this case it's a direct-list initialization. 更具体地说,在这种情况下,它是直接列表初始化。

Basically the m_font variable is initialized with the value that's given in the curly braces, in this case it's initialized to a shared_ptr for the font object that's given to the constructor. 基本上, m_font变量是使用花括号中给出的值初始化的,在这种情况下,它被初始化为提供给构造函数的font对象的shared_ptr

Font类具有一个名为m_font _ std::shared_ptr<sf::Font>类型的m_font成员,因此在Font类的构造函数中,该成员正在使用共享的font指针进行初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 C++ 构造函数初始值设定项列表中使用括号或花括号 - Use of parenthesis or curly braces in C++ constructor initializer list 大括号构造函数更喜欢使用initializer_list而不是更好的匹配。 为什么? - Curly braces constructor prefers initializer_list over better match. Why? 为什么采用std :: initializer_list的构造函数不是双花括号语法的首选 - Why wasn't a double curly braces syntax preferred for constructors taking a std::initializer_list 成员初始值设定项列表符号:花括号 vs 圆括号 - Member initializer list notation: curly braces vs parentheses 用花括号初始化 std::vector 使用初始化列表 - Initializing std::vector with curly braces uses initializer list C++ 队列<string>带有双花括号的初始化列表</string> - C++ queue<string> initializer list with double curly braces 复制构造函数花括号初始化 - Copy constructor curly braces initialization 使用花括号初始化程序的默认参数 - Default argument using curly braces initializer 为什么双空花括号{{}}创建一个std :: initializer_list <double> 有一个元素,不是零? - Why does double empty curly braces { { } } create a std::initializer_list<double> with one element, not zero? 带有 std::initializer_list 的奇怪构造函数 SFINAE 错误 - Weird constructor SFINAE error with std::initializer_list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM