简体   繁体   English

Function 被放在大括号中是为了初始化 class 中的成员。它的语法是什么?

[英]Function was put into a curly bracket in order to initialize the member in class. What is its syntax?

The code is shown here:代码显示在这里:

class Basket{
public:
/*other contents*/
private:
    // function to compare shared_ptrs needed by the multiset member
    static bool compare(const std::shared_ptr<Quote> &lhs,
        const std::shared_ptr<Quote> &rhs)
    { return lhs->isbn() < rhs->isbn(); }
    // multiset to hold multiple quotes, ordered by the compare member
    std::multiset<std::shared_ptr<Quote>, decltype(compare)*>
        items{compare};
};

We initialize our multiset through an in-class initializer.我们通过类内初始化器初始化我们的多重集。 Commonly we put a same class object in the curly bracket.通常我们将相同的 class object 放在花括号中。 Why a function can be put here?为什么这里可以放一个function呢? I can't understand;我不明白;

It's explained in the book C++ primer like it: The multiset will use a function with the same type as our compare member to order the elements.它在 C++ primer 书中解释如下:多重集将使用与我们的比较成员具有相同类型的 function 来对元素进行排序。 The multiset member is named items, and we're initializing items to use our compare function. multiset 成员名为 items,我们正在初始化 items 以使用我们的比较 function。

I can understand the logic, but what is the syntax used here?我能理解逻辑,但是这里使用的语法是什么?

items{compare}; is a call to one of the overloads of the constructor of std::mulitset .对 std::mulitset 的构造函数的重载之一的调用。

Which one of the overloads to use is decided by the compiler from looking at your argument type: compare matches the description of a "comparison function object" (see link), so the second invocation is used.使用哪一个重载由编译器根据您的参数类型决定: compare匹配“比较 function 对象”的描述(参见链接),因此使用第二次调用。

It is a pointer to a function, taking two values as parameters and returning a boolean is the main point here.它是一个指向一个function的指针,取两个值作为参数,返回一个boolean是这里的重点。 That function can then be later called by the multiset object to sort its members. function 随后可以由多重集 object 调用以对其成员进行排序。 Look up "callback functions" if you want to learn more about this concept.如果您想了解有关此概念的更多信息,请查找“回调函数”。 See here for a start: Callback functions in C++请参阅此处开始: C++ 中的回调函数

What you're refering to with你指的是什么

Commonly we put a same class object in the curly bracket.通常我们将相同的 class object 放在花括号中。

would be using the copy-constructor (no. 6 in the link).将使用复制构造函数(链接中的第 6 个)。 Just another way to create the same type of object.另一种创建相同类型 object 的方法。

There are questions here on SO about using a constructor with {} vs () if that is part of the confusion: What's the difference between parentheses and braces in c++ when constructing objects如果这是混淆的一部分,那么这里有关于使用带有{} vs ()的构造函数的问题: What's the difference between parentheses and braces in c++ when constructing objects

Commonly we put a same class object in the curly bracket.通常我们将相同的 class object 放在花括号中。 Why a function can be put here?为什么这里可以放一个function呢?

What actually happens is that compare when used inside the braces {} decays to a pointer to function due to type decay (as it is a static member function).实际发生的是,由于类型衰减(因为它是 static 成员函数),在大括号{}内使用时compare会衰减到指向 function 的指针。 Then this decayed function pointer is passed as argument to one of the std::multiset 's constructor which will make std::multiset to use this compare function as comparator.然后这个衰减的 function 指针作为参数传递给std::multiset的构造函数之一,这将使std::multiset使用这个compare function 作为比较器。

//--------------------------------vvvv-------------------->we're passing a pointer to static member function as argument for this first parameter
explicit multiset( const Compare& comp, const Allocator& alloc = Allocator() );

暂无
暂无

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

相关问题 错误:成员 function 不得在其 class 之外声明。 - Error: member function may not be declared outside of its class. C ++中的花括号语法和迭代器 - Curly bracket syntax and iterator in c++ 成员数组包含类的实例。 无法决定在哪里放置修改此类变量的方法 - A member array contains instances of a class. Can't decide where to put a method which modifies a variable of this class 哪个函数用于初始化静态类成员? - Which function is used to initialize the static class member? 嵌套类成员函数无法访问封闭类的功能。 为什么? - Nested Class member function can't access function of enclosing class. Why? 如何将任意类的任意成员函数传递给模板以解析其签名? - How do I pass arbitrary member function of ANY class to template in order to resolve its signature? 返回该类中定义的类型的类模板的成员函数的正确语法是什么? - What's the correct syntax for a member function of a class template returning a type defined in that class? 在另一个寺庙类中定义的模板类的静态对象。 在linux中运行会提供专门的成员&#39;require&#39;模板&lt;&gt;&#39;语法 - Static object of a templete class defined in another templete class. Running in linux gives specializing member ‘requires ‘template<>’ syntax 调用其中一个成员函数时,在对象中调用“初始化”函数 - Call an “initialize” function in an object when one of its member functions are called 重载括号运算符作为成员函数 - Overloading bracket operator as member function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM