// functionpointerparameterandoperator.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
using namespace std;
typedef struct pos
{
int x;
int y;
}pos;
pos operator+(pos a, pos b)// 重载运算符定义
{
pos p = {a.x + b.x, a.y + b.y};
return p;
}
pos add(pos a, pos b)
{
return a + b;// 重载运算符使用
}
pos operate(pos a, pos(* subfunc)(pos, pos), pos b)// 函数指针形参
{
return (* subfunc)(a, b);// 使用函数指针,也可以写成:return subfunc(a, b);
}
int _tmain(int argc, _TCHAR* argv[])
{
pos a, b;
char c;
cin>>a.x>>a.y>>c>>b.x>>b.y;
pos cur;
switch(c)
{
case '+':
cur = operate(a, add, b);// 函数实参
break;
default:
break;
}
cout<<cur.x<<' '<<cur.y<<endl;
cin>>c;
return 0;
}
函数指针作参数&运算符重载
最新推荐文章于 2024-04-25 18:40:56 发布