|
沙发
楼主 |
发表于 2011-8-18 09:51:00
|
只看该作者
成员函数重载一元运算符
也可以重载一元运算符,比如 ++ ,-- 或者别的。
当使用成员函数来重载一元运算符时,并没有对象被显式地传递给运算符。事实上,this 指针被作为参数隐式地传递给运算符函数,而运算将作用于调用这个函数的对象。如下面程序所示:
[C++] 纯文本查看 复制代码 #include <iostream>
using namespace std;
class three_d {
int x, y, z; //三维坐标的三个坐标值
public:
three_d() { x = y = z = 0; }
three_d(int i, int j, int k) { x = i; y = j; z = k; }
three_d operator+(three_d op2); //op1被隐式指定
three_d operator=(three_d op2); //op1被隐式指定
three_d operator++(); //前缀++
void show();
};
//重载 '+' 运算符
three_d three_d::operator+(three_d op2)
{
three_d temp;
temp.x = x + op2.x; //做加法运算,此处的 + 号为普通的运算符号
temp.y = y + op2.y;
temp.z = z + op2.z;
return temp;
}
//重载赋值运算符 '='
three_d three_d::operator=(three_d op2)
{
x = op2.x; //这里的 '=' 符号保持原始含义,为普通赋值运算符
y = op2.y;
z = op2.z;
return *this;
}
//重载前缀++,函数里没有参数
three_d three_d::operator++()
{
x++; //对 3 个坐标值进行增量运算
y++;
z++;
return *this;
}
//输出 x, y, z 的坐标值
void three_d::show()
{
cout << x << ", ";
cout << y << ", ";
cout << z << "\\n";
}
int main()
{
three_d a(1, 2, 3), b(10, 10, 10), c;
a.show();
b.show();
c = a + b; //将对象 a 和 b 相加
c.show();
c = a + b + c; //将对象 a, b, c 相加
c.show();
c = b = a; //多重赋值运算
c.show();
b.show();
++c; //对 c 做增量运算
c.show();
return 0;
}
运行输出:$ ./rloperator2
1, 2, 3
10, 10, 10
11, 12, 13
22, 24, 26
1, 2, 3
1, 2, 3
2, 3, 4 上面重载的是 '++' 运算符的前缀形式,也可以重载它的后缀形式。重载 '++' 的后缀形式如下所示:
[Plain Text] 纯文本查看 复制代码 three_d three_d::operator++(int notused);
notused 参数没有被函数使用,将被忽略。这个参数只是让编译器识别增量运算符是前缀形式还是后缀形式。下面代码还添加了类 three_d 中运算符 ++ 的后缀形式重载函数:
[C++] 纯文本查看 复制代码 #include <iostream>
using namespace std;
class three_d {
int x, y, z; //三维坐标的三个坐标值
public:
three_d() { x = y = z = 0; }
three_d(int i, int j, int k) { x = i; y = j; z = k; }
three_d operator+(three_d op2); //op1被隐式指定
three_d operator=(three_d op2); //op1被隐式指定
three_d operator++(); //前缀++
three_d operator++(int notused); //++的后缀形式
void show();
};
//重载 '+' 运算符
three_d three_d::operator+(three_d op2)
{
three_d temp;
temp.x = x + op2.x; //做加法运算,此处的 + 号为普通的运算符号
temp.y = y + op2.y;
temp.z = z + op2.z;
return temp;
}
//重载赋值运算符 '='
three_d three_d::operator=(three_d op2)
{
x = op2.x; //这里的 '=' 符号保持原始含义,为普通赋值运算符
y = op2.y;
z = op2.z;
return *this;
}
//重载前缀++,函数里没有参数
three_d three_d::operator++()
{
x++; //对 3 个坐标值进行增量运算
y++;
z++;
return *this;
}
three_d three_d::operator++(int notused)
{
three_d temp = *this;
x++;
y++;
z++;
return temp; //返回原始值
}
//输出 x, y, z 的坐标值
void three_d::show()
{
cout << x << ", ";
cout << y << ", ";
cout << z << "\\n";
}
int main()
{
three_d a(1, 2, 3), b(10, 10, 10), c;
a.show();
b.show();
c = a + b; //将对象 a 和 b 相加
c.show();
c = a + b + c; //将对象 a, b, c 相加
c.show();
c = b = a; //多重赋值运算
c.show();
b.show();
++c; //对 c 做增量运算
c.show();
c++; //后缀增量运算
c.show();
a = ++c; //a 得到 c 增加之后的值,因此 a 和 c 是相等的
a.show();
c.show();
a = c++; //a 得到 c 增量之前的值,因此 a 和 c 是不相等的
a.show();
c.show();
return 0;
}
运行输出:$ ./rloperator3
1, 2, 3
10, 10, 10
11, 12, 13
22, 24, 26
1, 2, 3
1, 2, 3
2, 3, 4
3, 4, 5
4, 5, 6
4, 5, 6
4, 5, 6
5, 6, 7 上面程序中,如果运算符 ++ 在对象之前,那么函数 operator++() 将被调用;反之,那么函数 operator++(int notused) 将被调用。同样的规则适用于类的减量运算符。
不能重载的运算符有: |
|