Chinaunix首页 | 论坛 | 博客
  • 博客访问: 775294
  • 博文数量: 239
  • 博客积分: 60
  • 博客等级: 民兵
  • 技术积分: 1045
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-22 18:25
文章分类

全部博文(239)

文章存档

2019年(9)

2018年(64)

2017年(2)

2016年(26)

2015年(30)

2014年(41)

2013年(65)

2012年(2)

分类: C/C++

2014-01-28 17:36:35

       Call  to  object  of  class  type    [over.call.object]  ( ISO/IEC 14882:2011(E)  § 13.3.1.1.2 )

      1   If the primary-expression  E  in the function call syntax evaluates to a class object of type “cv T”, then the set of candidate functions includes at least the function call perators of  T. The function call operators of T are obtained by ordinary lookup of the name operator() in the context of (E).operator().

        In addition, for each non-explicit conversion function declared in T of the form operator  conversion-type-id  ( ) attribute-specifier-seqopt       cv-qualifier  ; where cv-qualifier is the same cv-qualification as, or a greater cv-qualification than, cv, and where conversion- type-id denotes the type “pointer to function of (P1,...,Pn) returning R ”, or the type “reference to pointer to function of (P1,...,Pn) returning R ”, or the type “reference to function of (P1,...,Pn) returning R ”, a surrogate call function with the unique name  call-function and having the form R  call-function  ( conversion-type-id F,     P1  a1,  ...    ,Pn  an) {   return   F  (a1,...     ,an);   } is also considered as a candidate function.  Similarly, surrogate call functions are added to the set of candidate functions for each non-explicit conversion function declared in a base class of  T provided the function is not hidden within T by another intervening declaration128 .

      3   If such a surrogate call function is selected by overload resolution, the corresponding conversion function will be called to convert E to the appropriate function pointer or reference, and the function will then be invoked with the arguments of the call.  If the conversion function cannot be called (e.g., because of an ambiguity), the program is ill-formed.

      4   The  argument  list  submitted  to  overload  resolution consists  of  the  argument  expressions  present  in  the function call syntax preceded by the implied object argument  (E).
[ Note: 
         When comparing the call against the function call operators, the implied object argument is compared against the implicit object parameter of the function call operator.  When comparing the call against a surrogate call function, the implied object argument  is  compared  against  the  ?rst  parameter  of  the  surrogate  call  function. The conversion  function from which the surrogate call function was derived will be used in the conversion sequence for that parameter since  it  converts  the  implied  object  argument  to  the  appropriate  function pointer  or  reference  required  by that ?rst parameter.     
— end note ]
[ Example:

点击(此处)折叠或打开

  1. // operator_overload_resolution.cpp
  2. //
  3. #include <iostream>

  4. using namespace std;

  5. int f1(int)
  6. {
  7.     cout<<"[int f1(int)] called"<<endl;
  8.     return true;
  9. }

  10. int f2(char *)
  11. {
  12.     cout<<"[int f2(char *)] called"<<endl;
  13.     return true;
  14. }

  15. typedef int (*fp1)(int);
  16. typedef int (*fp2)(char*);

  17. struct Caller
  18. {
  19.     operator fp1() { return f1;}
  20.     operator fp2() { return f2;}
  21.     int operator()(char*)
  22.     {
  23.         cout<<"[int operator()(int)] called"<<endl;
  24.         return true;
  25.     }

  26.     int call(int)
  27.     {
  28.         cout<<"[int call(int)] called"<<endl;
  29.         return true;
  30.     }
  31. };


  32. int main(int argc, char* argv[])
  33. {
  34.     Caller call;

  35.     int if1 = call(1); // calls f1 via pointer returned from conversion function
  36.     int if2 = call("this is caller f2");// calls f2 via pointer returned from conversion function

  37.     return 0;
  38. }
— end example ]
 [Result:

点击(此处)折叠或打开

  1. [int f1(int)] called
  2. [int operator()(int)] called
— end result ]


阅读(1282) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~