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 }
    有些目标看似很遥远,但只要付出足够多的努力,这一切总有可能实现!
  • 相关阅读:
    directshow filter中添加属性页
    (转)3G卡片在开发板上的详细解决方法
    Mean Shift 算法流程
    FloodFill(漫水填充)算法
    Camshift算法(1)
    jQuery click实现toggle(fn,fn)
    每天学点MVC 【Controller返回类型总结】
    jQuery empty在IE下不支持
    Meta标签详解【转载】
    JS格式化
  • 原文地址:https://www.cnblogs.com/Bravewtz/p/10335774.html
Copyright © 2011-2022 走看看