操作符++(后缀)的重载
来源:互联网 发布:除了淘宝还有什么 编辑:程序博客网 时间:2024/06/06 20:11
下面是一个++操作符重载的例子:
class Int
...{
public:
Int(int n = 0) : n_(n) ...{}
Int operator ++(int)
...{
Int temp(*this);
n_++;
return temp;
}
Int& operator ++()
...{
n_++;
return *this;
}
private:
int n_;
};
...{
public:
Int(int n = 0) : n_(n) ...{}
Int operator ++(int)
...{
Int temp(*this);
n_++;
return temp;
}
Int& operator ++()
...{
n_++;
return *this;
}
private:
int n_;
};
初看上面的代码,觉得没有什么问题。但我们运行一下下面的代码:
int main()
...{
int i = 5, j = 10;
// 此行编译不过,因为i++是一个右值,不可以作为左值来使用
i++ = j++;
++i = ++j;
Int A = 10, B = 20;
// 此行编译通过
A++ = B++;
++A = ++B;
return getchar();
}
...{
int i = 5, j = 10;
// 此行编译不过,因为i++是一个右值,不可以作为左值来使用
i++ = j++;
++i = ++j;
Int A = 10, B = 20;
// 此行编译通过
A++ = B++;
++A = ++B;
return getchar();
}
在i++ = j++;中,我们知道,i++之后返回的是一个临时变量,而内置类型的临时变量是无法做左值的。因此,该行编译出错。但后面的A++ = B++;却能通过编译,这是为什么呢?
原来,A++返回的虽然也是一个临时变量,但该临时变量在该表达式结束之前有效,因此并没有象内置类型那样出现编译错误。
是的,我们应该避免此类问题,我们类的操作符++应该与内置类型的表现一致。我们只需这样修改:
class Int
...{
public:
Int(int n = 0) : n_(n) ...{}
// add const to avoid things like a++++;
const Int operator ++(int)
...{
Int temp(*this);
n_++;
return temp;
}
Int& operator ++()
...{
n_++;
return *this;
}
private:
int n_;
};
...{
public:
Int(int n = 0) : n_(n) ...{}
// add const to avoid things like a++++;
const Int operator ++(int)
...{
Int temp(*this);
n_++;
return temp;
}
Int& operator ++()
...{
n_++;
return *this;
}
private:
int n_;
};
返回一个const,就避免了这种不一致。
- 操作符++(后缀)的重载
- 前缀操作符和后缀操作符的重载
- 操作符重载,区分前缀与后缀操作符
- 重载前缀和后缀自增操作符
- 操作符的重载
- 操作符的重载
- 操作符的重载
- 操作符的重载
- 操作符的重载
- 操作符的重载
- 重载操作符的学习(一)
- 操作符的重载(上)
- 操作符的重载(下)
- 成员访问(->)操作符的重载
- 前缀++ 后缀++ 运算符重载
- 前缀++ 后缀++ 运算符重载
- 前缀++ 后缀++ 运算符重载
- 运算符重载 前缀++ 后缀++
- debian下关闭ipv6
- 无意中走上了SAP道路
- 一些 LOGO
- 深入理解++操作符
- 放出两只小猪共祝大家新年快乐!(利用C#直接绘制的哟)
- 操作符++(后缀)的重载
- 决定开始翻译McGraw.Hill.Osborne《Excel VBA Macro Programming》
- 对登陆filter的研究
- TCP协议规范
- Web2.0的腾飞
- 西门子实习感想(8)
- 在那玫瑰盛开的地方
- Objective of This Book-本书的目的
- 《.NET大局观》的一些阅读感悟与心得体会