zoukankan      html  css  js  c++  java
  • 运算符重载

    1. 一元运算符重载:运算符重载就是函数重载;函数重载通常会有两种方式来实现:友元函数和成员函数;重载运算符:operator
    • 负号重载(-):

    成员函数实现运算符重载的实例代码

     1 class Coordinate{
     2 private 3   int mX;
     4   int mY;
     5 public:
     6   Coordinate& operator-();//成员函数实现运算符重载
     7 }
     8 Coordinate& Coordinate::operator-(){
     9      mX=mX-1;
    10      mY=mY-1;
    11      return *this;
    12 }

     

    • ++重载(前置/后置):

    成员函数实现前置和后置++重载(声明文件:Coordinate.h):

     

     1 #ifndef COORDINATE_H_INCLUDED
     2 #define COORDINATE_H_INCLUDED
     3 class Coordinate{
     4 //friend Coordinate &operator(Coordinate &temp);
     5 
     6 public:
     7     Coordinate(int x,int y){
     8     mX=x;
     9     mY=y;
    10 }
    11     Coordinate &operator++();//前置(++)成员函数重载方式
    12     Coordinate operator++(int);//后置(++)成员函数重载方式,返回的不是引用而是一个对象,并且用参数int来标记其为后置++
    13     int getMx();
    14     int getMy();
    15 private:
    16     int mX;
    17     int mY;
    18 };
    19 #endif // COORDINATE_H_INCLUDED

     

    (实现文件:Coordinate.cpp):

     1 #include"Coordinate.h"
     2 Coordinate &Coordinate::operator++(){
     3     mX = mX+1;
     4     mY = mY+1;
     5     return *this;
     6 }
     7 Coordinate Coordinate::operator++(int){
     8     Coordinate co(*this);
     9     mX++;
    10     mY++;
    11     return co;
    12 }
    13 int Coordinate::getMx(){
    14     return mX;
    15 }
    16 int Coordinate::getMy(){
    17     return mY;
    18 }

         2. 二元运算符重载:也有成员函数重载和友元函数重载两种方式

    • <<运算符(只能够用友元函数进行重载,因为此时运算符的右边才是当前对象,如果是利用成员函数进行重载,那么当前对象必然在左边
     1 //Coordinate.h
     2 #ifndef COORDINATE_H_INCLUDED
     3 #define COORDINATE_H_INCLUDED
     4 #include <iostream>
     5 using namespace std;
     6 class Coordinate
     7 {
     8 
     9     friend ostream &operator<<(ostream &out,const Coordinate &co);
    10 public:
    11     Coordinate(int x,int y)
    12     {
    13         mX=x;
    14         mY=y;
    15     }
    16     Coordinate &operator++();//前置(++)成员函数重载方式
    17     Coordinate operator++(int);//后置(++)成员函数重载方式,返回的不是引用而是一个对象,并且用参数int来标记其为后置++
    18     int getMx();
    19     int getMy();
    20 private:
    21     int mX;
    22     int mY;
    23 };
    24 #endif // COORDINATE_H_INCLUDED
    //Coordinate.cpp
    #include"Coordinate.h"
    Coordinate &Coordinate::operator++(){
        mX = mX+1;
        mY = mY+1;
        return *this;
    }
    Coordinate Coordinate::operator++(int){
        Coordinate co(*this);
        mX++;
        mY++;
        return co;
    }
    ostream &operator<<(ostream &out,const Coordinate &co){
        out<<co.mX<<","<<co.mY;
       return out;
    }
    int Coordinate::getMx(){
        return mX;
    }
    int Coordinate::getMy(){
        return mY;
    }
    • []索引运算符(只能够用成员函数重载,不能够用友元函数进行重载,原因是当前对象只能作为运算符左边的操作对象进行操作
    1  int operator[](int index);//在当前对象的类中声明,在Coordinate.h中
    2 //关于重载函数的实现,在Coordinate.cpp中
    3 int Coordinate::operator[](int index){
    4     if(index==0)return mX;
    5     else return mY;
    6 }

    小结:利用成员函数实现运算符重载和利用友元函数实现运算符重载仍旧是有所区别的,如果说当前对象的位置只能在运算符左边,必然只能使用成员函数来实现重载;若是只能在右边,则必然只能私用友元函数来实现;若是说两个操作数的对象可以互换,那么两种实现方式均可;通常情况下,我们默认一元运算符的重载用成员函数实现,二元运算符的重载用友元函数来实现(当然肯定也是有特例的)。

  • 相关阅读:
    Flash、Ajax各自的优缺点,在使用中如何取舍?
    纯CSS气泡框实现方法探究
    CSS里padding和margin的区别是什么?
    img图片标签alt和title属性的区别
    JS中都有哪些数据类型呢?
    剑指offer---按只字形顺序打印二叉树
    剑指offer---序列化二叉树
    剑指offer---二叉树的下一个结点
    剑指offer---二叉树的深度
    剑指offer---平衡二叉树
  • 原文地址:https://www.cnblogs.com/upup-2015/p/5040830.html
Copyright © 2011-2022 走看看