c++ - 调用运算符和默认构造函数

浏览:57日期:2023-04-06

问题描述

关于符号(),它不是用在类名后面作用是调用默认构造函数,用在类的对象后边作用是函数调用运算符嘛。但是在书里边有这样一行代码sort(svec.begin(),svec.end(),greater<string>());为嘛说它是调用给定的greater函数对象呢?

问题解答

回答1:

意思是greater<string>()返回一个调用默认构造函数构造的函数对象

回答2:

greater是class template,内部重载了运算符operator()。另外,函数对象的定义如下function objects are instances of a class with member operator() defined. this member function allows the object to be used with the same syntax as a function call.所以,greater的对象是函数对象。greater<string>(),调用了的默认构造函数,生成一个临时对象。

相关文章: