当前位置: 首页 > wzjs >正文

湛江免费建站哪里有友情链接交换

湛江免费建站哪里有,友情链接交换,大名专业做网站,微信网页上的网站怎么做C运算符重载全面总结 运算符重载是C中一项强大的特性,它允许程序员为自定义类型定义运算符的行为。以下是关于C运算符重载的详细总结: 一、基本概念 1. 什么是运算符重载 运算符重载是指为自定义类型(类或结构体)重新定义或重…

C++运算符重载全面总结

运算符重载是C++中一项强大的特性,它允许程序员为自定义类型定义运算符的行为。以下是关于C++运算符重载的详细总结:

一、基本概念

1. 什么是运算符重载

运算符重载是指为自定义类型(类或结构体)重新定义或重载已有的运算符,使其能够操作该类型的对象。

2. 运算符重载的限制

  • 不能创建新的运算符
  • 不能改变运算符的优先级和结合性
  • 不能改变运算符的操作数个数
  • 部分运算符不能被重载(如::, .*, ., ?:等)
  • 重载运算符至少有一个操作数是用户定义类型

二、运算符重载的实现方式

1. 成员函数重载

运算符作为类的成员函数重载,隐含this指针作为第一个操作数。

class Complex {
public:Complex operator+(const Complex& other) const {return Complex(real + other.real, imag + other.imag);}
private:double real, imag;
};

2. 友元函数重载

运算符作为非成员函数重载,通常需要声明为友元以访问私有成员。

class Complex {friend Complex operator+(const Complex& a, const Complex& b);
};Complex operator+(const Complex& a, const Complex& b) {return Complex(a.real + b.real, a.imag + b.imag);
}

3. 全局函数重载

对于不访问私有成员的运算符,可以直接定义为全局函数。

Point operator+(const Point& a, const Point& b) {return Point(a.x() + b.x(), a.y() + b.y());
}

三、可重载运算符分类

1. 算术运算符

+, -, *, /, %, +=, -=, *=, /=, %=

class Complex {
public:Complex& operator+=(const Complex& other) {real += other.real;imag += other.imag;return *this;}
};

2. 关系运算符

==, !=, <, >, <=, >=

bool operator==(const Point& a, const Point& b) {return a.x() == b.x() && a.y() == b.y();
}

3. 逻辑运算符

!, &&, ||

class Boolean {
public:bool operator!() const { return !value; }
};

4. 位运算符

&, |, ^, ~, <<, >>, &=, |=, ^=, <<=, >>=

class BitMask {
public:BitMask operator<<(int shift) const {return BitMask(mask << shift);}
};

5. 赋值运算符

=, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=

class String {
public:String& operator=(const String& other) {// 实现深拷贝return *this;}
};

6. 递增递减运算符

++, --(前缀和后缀)

class Counter {
public:// 前缀++Counter& operator++() {++count;return *this;}// 后缀++Counter operator++(int) {Counter temp = *this;++count;return temp;}
};

7. 下标运算符

[]

class Array {
public:int& operator[](size_t index) {return data[index];}const int& operator[](size_t index) const {return data[index];}
};

8. 函数调用运算符

()

class Functor {
public:int operator()(int x, int y) {return x + y;}
};

9. 成员访问运算符

->, ->*

class SmartPtr {
public:T* operator->() const {return ptr;}
};

10. 类型转换运算符

operator type()

class Rational {
public:operator double() const {return static_cast<double>(numerator) / denominator;}
};

11. 内存管理运算符

new, new[], delete, delete[]

class MemoryPool {
public:void* operator new(size_t size) {return pool.allocate(size);}void operator delete(void* p) {pool.deallocate(p);}
};

四、特殊运算符重载注意事项

1. 输入输出运算符

<<, >> 通常应作为友元函数重载

class Complex {friend ostream& operator<<(ostream& os, const Complex& c);friend istream& operator>>(istream& is, Complex& c);
};ostream& operator<<(ostream& os, const Complex& c) {return os << c.real << "+" << c.imag << "i";
}

2. 赋值运算符

  • 应返回*this的引用以支持链式赋值
  • 需要处理自赋值情况
  • 通常需要实现拷贝赋值和移动赋值
class String {
public:String& operator=(const String& other) {if (this != &other) {// 实现深拷贝}return *this;}String& operator=(String&& other) noexcept {// 实现移动语义return *this;}
};

3. 类型转换运算符

  • 可以声明为explicit防止隐式转换
  • 应谨慎使用以避免意外的类型转换
class SafeBool {
public:explicit operator bool() const { return valid; }
};

五、运算符重载的最佳实践

  1. 保持直观性:重载的运算符行为应与内置类型相似
  2. 一致性:相关运算符应一起重载(如==!=<>等)
  3. 避免过度重载:只为有意义的操作重载运算符
  4. 考虑对称性:对于二元运算符,考虑是否需要支持交换操作数的顺序
  5. 性能考虑:尽可能使用引用和移动语义提高效率
  6. 异常安全:确保运算符重载是异常安全的

六、运算符重载示例

完整示例:复数类

class Complex {
public:Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}// 成员函数重载Complex operator+(const Complex& other) const {return Complex(real + other.real, imag + other.imag);}Complex& operator+=(const Complex& other) {real += other.real;imag += other.imag;return *this;}// 前缀++Complex& operator++() {++real;return *this;}// 后缀++Complex operator++(int) {Complex temp = *this;++real;return temp;}// 友元函数重载friend Complex operator-(const Complex& a, const Complex& b);friend std::ostream& operator<<(std::ostream& os, const Complex& c);// 比较运算符bool operator==(const Complex& other) const {return real == other.real && imag == other.imag;}bool operator!=(const Complex& other) const {return !(*this == other);}private:double real, imag;
};// 非成员函数实现
Complex operator-(const Complex& a, const Complex& b) {return Complex(a.real - b.real, a.imag - b.imag);
}std::ostream& operator<<(std::ostream& os, const Complex& c) {os << c.real << (c.imag >= 0 ? "+" : "") << c.imag << "i";return os;
}

七、C++20新增特性

1. 三路比较运算符 <=>

C++20引入了三路比较运算符,可以简化比较运算符的实现:

class Point {
public:auto operator<=>(const Point&) const = default;// 自动生成 ==, !=, <, <=, >, >=
};

2. 重载co_await

C++20协程支持重载co_await运算符。

总结

运算符重载是C++中实现自定义类型行为一致性的重要手段。合理使用运算符重载可以使代码更直观、更易读。但同时也要注意不要滥用,确保重载的运算符行为符合直觉,并遵循语言惯例。

http://www.dtcms.com/wzjs/192083.html

相关文章:

  • 龙岗区网站建设哪个公司好市场营销策划公司
  • php动态网站开发实训目的百度工具seo
  • 网站改版不换域名怎么做互联网营销师考试题及答案
  • 南通网站建设app推广引流方法
  • 做网站设计的价格关键词app
  • 标准网站建设推荐百度一下网页版浏览器
  • 甘南网站建设公司小米市场营销案例分析
  • 填空题ww秒懂2023西安关键词seo公司
  • 做网站 异地域名优化网站价格
  • 万网网站建设流程百度seo排名优化系统
  • seo优化器全网优化推广
  • 网站闭关保护怎么做网站怎么优化排名
  • 做网站需要买域名百度ai营销中国行
  • 建设银行日照分行官方网站企业短视频推广
  • 网站模板怎么用dreamweaver编辑百度网盘电脑版官网
  • 做软件外包的网站手机百度收录提交入口
  • 计算机网站建设是什么百度投诉中心24人工 客服电话
  • 青岛网站建设工作室织梦模板如何写好软文推广
  • 非法期货做网站南通企业网站制作
  • 海尔公司的网站建设b站推广费用一般多少
  • 连锁酒店的网站建设百度广告联盟平台
  • 2017两学一做竞赛网站短视频推广引流方案
  • wordpress视频调整seo实战密码第四版pdf
  • 宝宝发烧反反复复什么原因导致的seo网站优化网站编辑招聘
  • 浙江软装设计公司seo+网站排名
  • 电子商务网站建设管理实训报告站长素材
  • 内销网站要怎么做商丘搜索引擎优化
  • 35互联做的网站seo站内优化培训
  • 沈阳市建设监理协会网站网址怎么推广
  • 企业网站建立教程百度seo推广首选帝搜软件