博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
括号运算符重载
阅读量:4212 次
发布时间:2019-05-26

本文共 846 字,大约阅读时间需要 2 分钟。

() 运算符用于函数调用

重载格式        类型  :: operator()  ( 表达式表  ) ;

例1

        设 x 是类 X 的一个对象,则表达式

                 x ( arg1, arg2, … )

        可被解释为

                 x . operator () (arg1, arg2, … )

案例:

//例2:用重载()运算符实现数学函数的抽象

#include <iostream>

class  F

  { public : 

        double  operator ( )  ( double x ,  double  y ) ;

  } ;

double  F :: operator ( )  ( double  x ,  double  y )

   { return   x * x + y * y ; }

void main ( )                    

{

F  f  ;

f.getA();

      cout << f ( 5.2 , 2.5 ) << endl ;  // f . operator() (5.2, 2.5)

}

 

比较普通成员函数

 

代码如下,具体请看注释:

#include 
using namespace std;class F{public: int operator() (int a, int b) //()运算符重载 { return a * a + b * b; }};class F2{public: int MemFunc(int a, int b) //普通成员函数 { return a * a + b * b; }};//void main(){ F f; f(2, 4); //一般写成这种形式的,不是调用构造函数就是调用operator()函数,即括号运算符重载函数 F2 f2; f2.MemFunc(2, 4); //普通成员函数的调用 // //operator() (int a, int b) cout << "hello..." << endl; system("pause"); return;}

 

转载地址:http://krzmi.baihongyu.com/

你可能感兴趣的文章
九度OJ 1095:2的幂次方 (递归)
查看>>
九度OJ 1471-1480(10/10)
查看>>
九度OJ 1481-1490(7/10)
查看>>
九度OJ 1491-1500(5/10)
查看>>
九度OJ 1501-1510(10/10)
查看>>
业务系统中,报表统计功能如何组织--统计分析模块参考
查看>>
面向数据集成的ETL技术研究
查看>>
DataStage(ETL)技术总结 -- 介绍篇(转载)
查看>>
Greenplum技术浅析--vs oracle RAC
查看>>
框架一
查看>>
Oracle-内存管理解读
查看>>
Oracle-PFILE和SPFILE解读
查看>>
leetcode 13: Roman to Integer
查看>>
a标签中调用js方法
查看>>
js函数中传入的event参数
查看>>
[hive]优化策略
查看>>
c++14现代内存管理
查看>>
右值引用,move语义和完美转发
查看>>
c++使用宏检测类是否包含某个函数或者变量属性
查看>>
CSS之Multi-columns的column-gap和column-rule
查看>>