zoukankan      html  css  js  c++  java
  • 关于c++中前++后++运算符重载问题

     1 #include<iostream>
     2 using namespace std;
     3 
     4 class Complex{
     5 public:
     6     Complex(int a,int b){
     7         this->a=a;
     8         this->b=b;
     9     }
    10 
    11     void printComplex(){
    12         cout<<"("<<this->a<<","<<this->b<<"i)"<<endl;
    13     }
    14     friend Complex& operator++(Complex& c);/*友元函数声明*/
    15     friend const Complex operator++(Complex& c,int);
    16 /*
    17     局部中的重载前++运算符
    18     Complex& operator++(){
    19         this->a++;
    20         this->b++;
    21 
    22         return *this;
    23     }
    24 */
    25 /*
    26     局部中的重载后++运算符
    27     const Complex operator++(int){//亚元(即占位符,坑啊~~~)
    28         Complex temp(this->a,this->b);
    29         this->a++;
    30         this->b++;
    31         
    32         return temp;
    33     }
    34 */
    35 private:
    36     int a;
    37     int b;
    38 };
    39 
    40 /*重载前++运算符*/
    41 Complex& operator++(Complex& c){
    42     c.a++;
    43     c.b++;
    44     return c;
    45 }
    46 /*重载后++运算符,后++不可以多次进行*/
    47 const Complex operator++(Complex& c,int){//int 为占位符
    48     Complex temp(c.a,c.b);
    49     c.a++;
    50     c.b++;
    51 
    52     return temp;
    53 }
    54 
    55 int main(){
    56     Complex c1(1,2);
    57     ++++c1;//可以多次进行前置++
    58     c1.printComplex();
    59     c1++;
    60     c1.printComplex();
    61 
    62     return 0;
    63 }
    有些目标看似很遥远,但只要付出足够多的努力,这一切总有可能实现!
  • 相关阅读:
    PHP延迟静态绑定
    PHP SPL神器实现堆排序
    魔术方法__sleep 和 __wakeup
    SPL 笔记
    PHP中对象的深拷贝与浅拷贝
    PHP的垃圾回收机制详解
    深入理解PHP中赋值与引用
    xdebug安装及使用小结
    工作中碰到的一个问题(cookie相关)
    分享一个解析XML成为php数组的方法
  • 原文地址:https://www.cnblogs.com/Bravewtz/p/10335774.html
Copyright © 2011-2022 走看看