[英]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.