为什么运算符重载无法正常执行?
Why is the operator overloading not performing properly?
在 C++ 中重载运算符 ++ 时出现编译错误。这是代码:
#include <iostream>
using namespace std;
class Age{
private:
int age;
public:
Age(int age): age(age){
}
Age& operator++(){
Age ages(this->age + 1);
return ages;
}
int getAge(){
return age;
}
};
int main(){
Age myAge(20);
Age nextAge = myAge++;
cout << nextAge.getAge() << endl;
return 0;
}
我哪里弄错了?
operator++()
定义前置自增运算符。
要定义 post-increment 你需要声明 operator++(int)
int
参数实际上并没有被使用,但是需要一些语法方式来区分预增量和post-增量重载,所以它们有不同的签名。
不过您还有其他问题:您的运算符不修改 *this
它只修改局部变量,并且它 return 是对该局部变量的引用,当您尝试访问 return 值。
您可能想要定义一个预增量来修改 *this
和 returns 参考:
Age& operator++(){
this->age += 1;
return *this;
}
然后据此定义 post-increment,创建一个副本并按值 returning 它:
Age operator++(int){
Age age(*this); // make a copy of the current value
++*this; // update the current value
return age; // return the copy
}
你也用得很奇怪:
Age nextAge = myAge++;
nextAge
变量将不是下一个年龄,它将是myAge
的旧值,而myAge
将是增加到下一个值。尝试更改您的程序以使用简单的 int
变量并查看 ++
运算符的行为方式。
如果您不了解运算符的作用,则尝试创建自己的 operator++
重载毫无意义!
也许你真正想要的只是 operator+
所以你可以这样写:
Age nextAge = myAge + 1;
有几种方法可以定义 operator+
函数。给定上面的预递增运算符,您可以像这样将其定义为非成员函数:
Age operator+(const Age& age, int n) {
Age newAge(age);
while (n--) {
++newAge;
}
return newAge;
}
或者作为 (const) 成员函数更有效:
Age operator+(int n) const {
Age newAge(*this);
newAge->age += n;
return newAge;
}
您不应该在 operator++
重载中返回对局部变量的引用。这将导致未定义的行为。 Return 在这种情况下按值。
预自增运算符应如下所示:
Age& operator++()
{
++(this->age);
return *this; // you can safely return a reference to the object pointed by "this"
}
post-增量运算符应如下所示:
Age operator++(int) // the int argument is merely to distinguis the two operators - it has no other use
{
Age previousAge( this->age ); // the post-increment operator must return the value before incementation
++(this->age);
return previousAge; // you can safely return a copy of a local variable
}
此外,如果成员变量使用了Hungarian notation,则不需要使用"this"。
在 C++ 中重载运算符 ++ 时出现编译错误。这是代码:
#include <iostream>
using namespace std;
class Age{
private:
int age;
public:
Age(int age): age(age){
}
Age& operator++(){
Age ages(this->age + 1);
return ages;
}
int getAge(){
return age;
}
};
int main(){
Age myAge(20);
Age nextAge = myAge++;
cout << nextAge.getAge() << endl;
return 0;
}
我哪里弄错了?
operator++()
定义前置自增运算符。
要定义 post-increment 你需要声明 operator++(int)
int
参数实际上并没有被使用,但是需要一些语法方式来区分预增量和post-增量重载,所以它们有不同的签名。
不过您还有其他问题:您的运算符不修改 *this
它只修改局部变量,并且它 return 是对该局部变量的引用,当您尝试访问 return 值。
您可能想要定义一个预增量来修改 *this
和 returns 参考:
Age& operator++(){
this->age += 1;
return *this;
}
然后据此定义 post-increment,创建一个副本并按值 returning 它:
Age operator++(int){
Age age(*this); // make a copy of the current value
++*this; // update the current value
return age; // return the copy
}
你也用得很奇怪:
Age nextAge = myAge++;
nextAge
变量将不是下一个年龄,它将是myAge
的旧值,而myAge
将是增加到下一个值。尝试更改您的程序以使用简单的 int
变量并查看 ++
运算符的行为方式。
如果您不了解运算符的作用,则尝试创建自己的 operator++
重载毫无意义!
也许你真正想要的只是 operator+
所以你可以这样写:
Age nextAge = myAge + 1;
有几种方法可以定义 operator+
函数。给定上面的预递增运算符,您可以像这样将其定义为非成员函数:
Age operator+(const Age& age, int n) {
Age newAge(age);
while (n--) {
++newAge;
}
return newAge;
}
或者作为 (const) 成员函数更有效:
Age operator+(int n) const {
Age newAge(*this);
newAge->age += n;
return newAge;
}
您不应该在 operator++
重载中返回对局部变量的引用。这将导致未定义的行为。 Return 在这种情况下按值。
预自增运算符应如下所示:
Age& operator++()
{
++(this->age);
return *this; // you can safely return a reference to the object pointed by "this"
}
post-增量运算符应如下所示:
Age operator++(int) // the int argument is merely to distinguis the two operators - it has no other use
{
Age previousAge( this->age ); // the post-increment operator must return the value before incementation
++(this->age);
return previousAge; // you can safely return a copy of a local variable
}
此外,如果成员变量使用了Hungarian notation,则不需要使用"this"。