重载++前和后增量
Overloading ++ for both pre and post increment
本文关键字:重载 更新时间:2023-10-16
我们可以重载operator++
进行前递增和后递增吗?即调用SampleObject++
和++SampleObject
会得到正确的结果。
class CSample {
public:
int m_iValue; // just to directly fetch inside main()
CSample() : m_iValue(0) {}
CSample(int val) : m_iValue(val) {}
// Overloading ++ for Pre-Increment
int /*CSample& */ operator++() { // can also adopt to return CSample&
++(*this).m_iValue;
return m_iValue; /*(*this); */
}
// Overloading ++ for Post-Increment
/* int operator++() {
CSample temp = *this;
++(*this).m_iValue;
return temp.m_iValue; /* temp; */
} */
};
不能只根据返回类型重载函数,而且即使允许这样做,也不能解决问题,因为重载解析存在歧义。
既然提供了操作符重载使内置类型表现得像用户定义的类型,为什么不能同时对自己的类型使用前置和后增量呢?
为了消除歧义,自增运算符的后缀版本接受一个假int
参数:
// prefix
CSample& operator++()
{
// implement increment logic on this instance, return reference to it.
return *this;
}
// postfix
CSample operator++(int)
{
CSample tmp(*this);
operator++(); // prefix-increment this instance
return tmp; // return value before increment
}
类型T
的递增前和递增后的标准模式T& T::operator++() // pre-increment, return *this by reference
{
// perform operation
return *this;
}
T T::operator++(int) // post-increment, return unmodified copy by value
{
T copy(*this);
++(*this); // or operator++();
return copy;
}
(您也可以调用一个通用函数来执行增量,或者如果它是一个简单的单行代码,如对成员的++,则只需同时执行)
为什么我们不能同时对自己的类型使用前后自增操作
您可以:
class CSample {
public:
int m_iValue;
CSample() : m_iValue(0) {}
CSample(int val) : m_iValue(val) {}
// Overloading ++ for Pre-Increment
int /*CSample& */ operator++() {
++m_iValue;
return m_iValue;
}
// Overloading ++ for Post-Increment
int operator++(int) {
int value = m_iValue;
++m_iValue;
return value;
}
};
#include <iostream>
int main()
{
CSample s;
int i = ++s;
std::cout << i << std::endl; // Prints 1
int j = s++;
std::cout << j << std::endl; // Prints 1
}
[N4687]
16.5.7
用户定义函数operator++实现了前缀++和后缀++操作符。如果此函数是没有形参的非静态成员函数,或者只有一个形参的非成员函数,则为该类型的对象定义前缀自增操作符++。如果函数是带一个形参(int类型)的非静态成员函数或带两个形参(第二个形参为int类型)的非成员函数,则为该类型的对象定义后缀自增操作符++。当使用++操作符调用后缀自增时,int实参的值为0
例子:
struct X {
X& operator++(); // prefix ++a
X operator++(int); // postfix a++
};
struct Y { };
Y& operator++(Y&); // prefix ++b
Y operator++(Y&, int); // postfix b++
void f(X a, Y b) {
++a; // a.operator++();
a++; // a.operator++(0);
++b; // operator++(b);
b++; // operator++(b, 0);
a.operator++(); // explicit call: like ++a;
a.operator++(0); // explicit call: like a++;
operator++(b); // explicit call: like ++b;
operator++(b, 0); // explicit call: like b++;
}
相关文章:
- 继承函数的重载解析
- 你能重载对象变量名本身返回的内容吗
- 从父命名空间重载类型
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 重载操作程序时出错>>用于类中的字符串 memebr
- 一个关于在C++中重载布尔运算符的问题
- 不同翻译单元中不可重载的非内联函数定义
- 为什么使用SFINAE而不是函数重载
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- 将重载的成员函数传递给函数模板
- c++:可变模板和函数重载
- 重载元组索引运算符-C++
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 重载==不适用于二进制树
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 重载运算符new[]的行为取决于析构函数
- 正在尝试重载二进制搜索树分配运算符
- 重载Singly Linked List中的赋值运算符
- 取消引用运算符不能重载