重载函数

View Code
#include<iostream>
using namespace std;

struct pos {
    int x ,y ;
    pos() {};
    pos(int X,int Y) :x(X) , y(Y) {} 
    pos operator + (pos & a) {         //重载 + 号,可以直接相加
        return pos(a.x + x,a.y + y);
    }
    void print() {
        cout << x << " " << y <<endl;
    }
};

template <typename T>                 //重载函数,中间不能加东西
T add(T a,T b) {
    return a + b;
}

inline int max (int a,int b) {       //inline相当于之间进函数,没有消耗调用函数的时间
    return a>b? a:b;
}

int main() {
    int a = 3, b = 2;
    cout << add(a,b) << endl;
    double da = 1.9, db = 2.9;
    cout << add(da,db) << endl;
    pos ans;
    pos pa ,pb ;
    pa = pos(1,2);
    pb = pos(3,4);
    ans = add(pa ,pb );
    ans.print();
    return 0;
}

inline和重载的运用实例

原文地址:https://www.cnblogs.com/gray035/p/2969645.html