做期货财经网站需要哪些资质,wordpress add_options_page,WordPress 种子搜索,长沙建网站企业C学习第十四课#xff1a;运算符类型与运算符重载
在C中#xff0c;运算符重载是一种使得自定义类型#xff08;如类对象#xff09;能够使用C内建运算符的能力。运算符重载允许程序员定义运算符对用户定义类型的特殊行为#xff0c;这增加了程序的可读性和自然表达能力。…
C学习第十四课运算符类型与运算符重载
在C中运算符重载是一种使得自定义类型如类对象能够使用C内建运算符的能力。运算符重载允许程序员定义运算符对用户定义类型的特殊行为这增加了程序的可读性和自然表达能力。本课将详细介绍C中的运算符类型、运算符重载的概念、语法和示例。
1. C中的运算符类型
C提供了多种运算符它们可以按功能分为以下几类
算术运算符, -, *, /, %取余。关系运算符, , , , , !。逻辑运算符逻辑与, ||逻辑或, !逻辑非。位运算符, |, ^, ~, 左移, 右移。赋值运算符, , -, *, /, % 等。其他运算符sizeof, new, delete 等。
2. 运算符重载的概念
运算符重载允许改变运算符对用户定义类型的行为。C支持以下几种重载方式
成员函数将运算符重载为类的成员函数。友元函数将运算符重载为类的友元函数。转换运算符允许类对象被视为其他类型。
3. 运算符重载的语法
运算符重载可以通过成员函数或友元函数实现。以下是重载加法运算符为友元函数的示例
class Vector {
public:int x, y;// 友元函数重载加法运算符friend Vector operator(const Vector v1, const Vector v2);
};Vector operator(const Vector v1, const Vector v2) {return Vector(v1.x v2.x, v1.y v2.y);
}4. 成员函数重载运算符
运算符也可以作为类的成员函数被重载
class Complex {
public:double real, imag;// 成员函数重载加法运算符Complex operator(const Complex rhs) const {return Complex(real rhs.real, imag rhs.imag);}
};5. 输入输出流运算符重载
重载输入输出流运算符可以方便地读写类对象
std::ostream operator(std::ostream os, const Vector v) {os ( v.x , v.y );return os;
}std::istream operator(std::istream is, Vector v) {is v.x v.y;return is;
}6. 关系运算符重载
关系运算符常用于比较类对象
bool operator(const Vector lhs, const Vector rhs) {return (lhs.x rhs.x) ? lhs.y rhs.y : lhs.x rhs.x;
}7. 赋值运算符重载
赋值运算符允许定义类对象的赋值行为
class MyClass {
public:int value;MyClass operator(const MyClass other) {if (this ! other) {value other.value;// 深拷贝资源如果有的话}return *this;}
};8. 运算符重载与构造函数
构造函数不能被重载但可以模拟运算符的行为
class String {
public:char* str;String(const char* s) {str new char[strlen(s) 1];strcpy(str, s);}// 模仿赋值运算符的构造函数String operator(const String rhs) {if (this ! rhs) {delete[] str;str new char[strlen(rhs.str) 1];strcpy(str, rhs.str);}return *this;}~String() {delete[] str;}
};9. 运算符重载的规则
运算符重载不能改变运算符的语法或优先级。不能重载所有的运算符如 ., ::, .*, sizeof 等。运算符重载应保持其原有的语义。
10. 特殊运算符重载函数调用运算符 operator()
函数调用运算符允许类对象表现得像函数一样
class MathFunc {
public:// 重载函数调用运算符double operator()(double x) const {return x * x 2 * x 1;}
};11. 特殊运算符重载下标运算符 operator[]
下标运算符允许使用数组的下标访问类对象
class Array {int* data;int size;
public:int operator[](int index) {return data[index];}const int operator[](int index) const {return data[index];}
};12. 运算符重载与类型转换
运算符重载可以用于类型转换
class Rational {int numerator, denominator;
public:// 类似于隐式类型转换的运算符重载operator double() const {return static_castdouble(numerator) / denominator;}
};结语
通过本课的学习你了解了C中运算符重载的概念、语法和示例。运算符重载是C的一个强大特性它使得自定义类型能够使用C内建运算符提高了程序的表达能力。
正确使用运算符重载可以增加代码的可读性和一致性但滥用运算符重载会使代码变得难以理解和维护。因此运算符重载应遵循明确、一致和可预测的原则。