zoukankan      html  css  js  c++  java
  • increment/decrement/dereference操作符

    标题以上分别对于++/--/*

     1 #include <iostream>
     2 #include <cstddef>
     3 
     4 using namespace std;
     5 
     6 class INT {
     7     friend ostream& operator<<(ostream& os, const INT& i);
     8 
     9 private:
    10     int m_i;
    11 
    12 public:
    13     INT(int i) :
    14             m_i(i) {
    15     }
    16     //前缀
    17     INT& operator++() {
    18         cout << "++i" << endl;
    19         ++(this->m_i);
    20         return *this;
    21     }
    22     //后缀
    23     const INT operator++(int) {
    24         //const的作用是防止i++++
    25         //i++++=i.operator++(0).operator++(0)
    26         //第二次++操作的完全就是第一次的临时变量
    27         //而且int也不允许这样写
    28         /*<html>https://blog.csdn.net/piaopiaohu123/article/details/7333771</html>*/
    29         cout << "i++" << endl;
    30         INT temp = *this;
    31         ++(*this);
    32         return temp;
    33     }
    34 
    35     //前缀
    36     INT& operator--() {
    37         cout << "--i" << endl;
    38         --(this->m_i);
    39         return *this;
    40     }
    41 
    42     //后缀
    43     const INT operator--(int) {
    44         cout << "i--" << endl;
    45         INT temp = *this;
    46         ++(*this);
    47         return temp;
    48     }
    49 
    50     int & operator*() const {
    51         return (int&) m_i;
    52         //以上转换操作告诉编译器,你确实要将const int转为non-const lvalue
    53         //如果没有这样明白地转型,有些编译器会给你警告,有些更严格的编译器会视为错误
    54     }
    55 
    56     //写在内部只能有一个参数,另一个参数是this
    57 //    ostream& operator<<(ostream& os, const INT& i) {
    58 //        os << '[' << i.m_i << ']';
    59 //        return os;
    60 //    }
    61 };
    62 ostream& operator<<(ostream& os, const INT& i) {
    63     os << '[' << i.m_i << ']';
    64     return os;
    65 }
    66 
    67 void A(int &a) {
    68 
    69 }
    70 
    71 int main(int argc, char **argv) {
    72     INT I(5);
    73 //    cout << I++ << endl;
    74     //测试后缀的临时变量的效果
    75     //int i=5;
    76     //A(i++);//error:说明i++返回的也是临时变量
    77     //A(++i);//可以
    78     /*
    79      * void A(const int &a)
    80      * 都可以
    81      */
    82 
    83     //测试operator*()返回值的引用效果
    84     cout << ++(*I) << endl;    //6
    85     cout << I << endl;        //[6]
    86     int &a = *I;
    87     cout << ++a << endl;    //7
    88     cout << I << endl;        //[7]
    89     int b = *I;
    90     cout << ++b << endl;    //8
    91     cout << I << endl;        //[7]
    92 
    93 //    cout << ++I << endl;
    94 //    cout << I << endl;
    95     return 0;
    96 }
    少壮不识cpp,老大方知cpp可怕
  • 相关阅读:
    tornado学习
    pycharm使用技巧
    k8s排查问题利器-----kubectl debug
    linux的python3 环境配置
    flink入门学习
    jenkins deploy key的使用
    通过浏览器模拟网速慢,接口block等异常情况
    curl常用语法举例
    jmeter测试双向认证https接口
    一些开发的总结
  • 原文地址:https://www.cnblogs.com/Jacket-K/p/9284589.html
Copyright © 2011-2022 走看看