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

 #include <iostream>
#include <cstddef> using namespace std; class INT {
friend ostream& operator<<(ostream& os, const INT& i); private:
int m_i; public:
INT(int i) :
m_i(i) {
}
//前缀
INT& operator++() {
cout << "++i" << endl;
++(this->m_i);
return *this;
}
//后缀
const INT operator++(int) {
//const的作用是防止i++++
//i++++=i.operator++(0).operator++(0)
//第二次++操作的完全就是第一次的临时变量
//而且int也不允许这样写
/*<html>https://blog.csdn.net/piaopiaohu123/article/details/7333771</html>*/
cout << "i++" << endl;
INT temp = *this;
++(*this);
return temp;
} //前缀
INT& operator--() {
cout << "--i" << endl;
--(this->m_i);
return *this;
} //后缀
const INT operator--(int) {
cout << "i--" << endl;
INT temp = *this;
++(*this);
return temp;
} int & operator*() const {
return (int&) m_i;
//以上转换操作告诉编译器,你确实要将const int转为non-const lvalue
//如果没有这样明白地转型,有些编译器会给你警告,有些更严格的编译器会视为错误
} //写在内部只能有一个参数,另一个参数是this
// ostream& operator<<(ostream& os, const INT& i) {
// os << '[' << i.m_i << ']';
// return os;
// }
};
ostream& operator<<(ostream& os, const INT& i) {
os << '[' << i.m_i << ']';
return os;
} void A(int &a) { } int main(int argc, char **argv) {
INT I();
// cout << I++ << endl;
//测试后缀的临时变量的效果
//int i=5;
//A(i++);//error:说明i++返回的也是临时变量
//A(++i);//可以
/*
* void A(const int &a)
* 都可以
*/ //测试operator*()返回值的引用效果
cout << ++(*I) << endl; //
cout << I << endl; //[6]
int &a = *I;
cout << ++a << endl; //
cout << I << endl; //[7]
int b = *I;
cout << ++b << endl; //
cout << I << endl; //[7] // cout << ++I << endl;
// cout << I << endl;
return ;
}

increment/decrement/dereference操作符的更多相关文章

  1. STL——increment/decrement/dereference操作符

    increment/dereference操作符在迭代器的实现上占有非常重要的地位,因为任何一个迭代器都必须实现出前进(increment,operator++)和取值(dereference,ope ...

  2. increment/decrement/dereference

    #include <vector> #include <deque> #include <algorithm> #include <iostream> ...

  3. ITEM M6 自增(INCREMENT)、自减(DECREMENT)操作符前缀形式与后缀形式的区别

    前缀自增 UPInt & UPint::operator++() { *this+=1; return *this; } 后缀自增 const UPInt & UPint::opera ...

  4. 【M6】区别increment/decrement操作符的前置(prefix)和后置(postfix)形式

    1.考虑++(--的情况是一样的),前置是累加然后取出,后置是取出然后累加. 2.重载方法根据形参表的不同区分,问题来了,前置和后置形式都没有形参,因此没法区分.怎么办? 对于后置增加一个形参int, ...

  5. [atAGC049E]Increment Decrement

    由于每一个操作的逆操作都存在,可以看作将$a_{i}$全部变为0的代价 先考虑第一个问题,即对于确定的$a_{i}$如何处理 如果仅能用第2种操作,定义点$i$的代价为以$i$为左端点或以$i-1$为 ...

  6. noobSTL-1-配置器-1

    noobSTL-1-配置器-1 1.要点分析 1.1 可能让你困惑的C++语法 组态 即配置. 临时对象 一种无名对象.有时候会刻意地制造临时对象. 静态常量整数成员在class内部直接初始化 con ...

  7. noobSTL-1-配置器-0

    noobSTL-1-配置器-0 0.前言 STL的配置器(allocator),也叫内存分配器,负责空间配置与管理,简单地说,就是负责管理内存的. 从实现的角度来看,配置器是一个实现了动态空间配置.空 ...

  8. 《STL源码剖析》学习半生记:第一章小结与反思

    不学STL,无以立.--陈轶阳 从1.1节到1.8节大部分都是从各方面介绍STL, 包括历史之类的(大致上是这样,因为实在看不下去我就直接略到了1.9节(其实还有一点1.8.3的内容)). 第一章里比 ...

  9. C++ 重载操作符与转换

    <C++ Primer 4th>读书笔记 重载操作符是具有特殊名称的函数:保留字 operator 后接需定义的操作符号. Sales_item operator+(const Sales ...

随机推荐

  1. k8s使用Glusterfs动态生成pv

    一.环境介绍 [root@k8s-m ~]# cat /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4 ...

  2. spring 启动脚本分析

    参考:JVM 参数使用总结 参考:java  -Xms -Xmx -XX:PermSize -XX:MaxPermSize 参考:JVM调优总结 -Xms -Xmx -Xmn -Xss 参考:JAVA ...

  3. Yii2总结

    1. Web访问流程(即在浏览器中输入一个网址至浏览器展现页面结果的过程) a. 将输入的网址提取出域名,在本地hosts文件中查找对应的IP地址(windows为C:/windows/system3 ...

  4. js外部调用layui.use中的函数的写法

    layui模块化的写法固然不错,但也有让人不适应的一些地方 外部调用函数的写法就让人不太舒服 需要在函数名前面加上window这个前缀,就不太舒服 补充:window前缀,是全局变量的声明方式 如下: ...

  5. css元素选择器

    css的元素选择器就是html的标签名:

  6. Java使用RabbitMQ之公平分发

    发送消息: package org.study.workfair; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Con ...

  7. java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=fun2], {ExactMatcher:fDisplayName=fun2(cn.itcast.demo2.fun1)], {LeadingIdentifierMatcher:fClassName=cn.itcast.demo2.fun1,fLeadi

    Junit报的错误, 在测试方法前面没有添加注解@Test

  8. Qt 网格布局

    把十六个button放到网格布局的界面上 #include "mainwindow.h" #include <QApplication> #include<QtW ...

  9. Civil 3D 二次开发 事务

    事务,一般是指要做的或所做的事情.在计算机术语中是指访问并可能更新数据库中各种数据项的一个程序执行单元(unit). 对于初学者来说,从字面上难以理解什么是事务.下面我试着通过讲述事务的作用及特性来帮 ...

  10. [USACO12MAR] 花盆Flowerpot

    类型:二分+单调队列 传送门:>Here< 题意:给出$N$个点的坐标,要求根据$x$轴选定一段区间$[L,R]$,使得其中的点的最大与最小的$y$值之差$\geq D$.求$Min\{R ...