C++之重载操作符
1.类中重载+操作符
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std; class Object
{
public:
Object()
{ } Object(int a)
{
num = a;
} Object operator + (const Object& a) //重载+操作符;
{
Object result;
result.num = num + a.num;
return result;
}
private:
int num;
}; int main(int argc, char * argv[])
{
int a = ;
int b = ; Object obja(a);
Object objb(b); Object objc = obja + objb; system("pause");
return ;
}
2.重载全局操作符
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std; class Object
{
friend Object operator + (const Object &a, const Object &b);
public:
Object()
{ } Object(int a)
{
num = a;
} private:
int num;
}; Object operator + (const Object &a, const Object &b)
{
Object result;
result.num = a.num + b.num;
return result;
} int main(int argc, char * argv[])
{
Object obja();
Object objb(); Object objc = obja + objb; system("pause");
return ;
}
全局操作符,主要注意的是,当重载操作符访问私有成员时,需要将操作符声明为友元;
4. 重载[] 操作符
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std; class Object
{
public:
Object()
{
} Object(char * str)
{
int size = strlen(str);
m_buf = new char[size];
strcpy(m_buf,str);
} char& operator [] (int index)
{
return m_buf[index];
} ~Object()
{
delete[] m_buf;
} private:
char *m_buf;
}; int main(int argc, char * argv[])
{
Object obja("weiyouqing"); char str = obja[];
obja[] = 'W'; system("pause");
return ;
}
5.重载==操作符
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std; class Object
{
public:
Object()
{
} Object(int a)
{
num = a;
} bool operator == (const Object &other)
{
if (num == other.num)
return true;
else
return false;
} ~Object()
{ } private:
int num;
}; int main(int argc, char * argv[])
{
Object obja();
Object objb();
if (obja == objb)
cout << "equal" << endl;
else
cout << "not equal" << endl; system("pause");
return ;
}
全局重载操作符:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std; class Object
{
friend bool operator == (const Object &a, const Object &b);
public:
Object()
{
} Object(int a)
{
num = a;
} ~Object()
{ } private:
int num;
}; bool operator == (const Object &a,const Object &b)
{
if (a.num == b.num)
return true;
else
return false;
} int main(int argc, char * argv[])
{
Object obja();
Object objb();
if (obja == objb)
cout << "equal" << endl;
else
cout << "not equal" << endl; system("pause");
return ;
}
C++之重载操作符的更多相关文章
- 《精通C#》索引器与重载操作符(11.1-11.2)
1.索引器方法结构大致为<modifier><return type> this [argument list],它可以在接口中定义: 在为接口声明索引器的时候,记住声明只是表 ...
- C++ 重载操作符与转换
<C++ Primer 4th>读书笔记 重载操作符是具有特殊名称的函数:保留字 operator 后接需定义的操作符号. Sales_item operator+(const Sales ...
- 解释清楚c++的重载操作符【用自己的话,解释清楚】
C++中对于内置的变量及标准库中常见的类定义类常见的操作符含义,对于自定义的类也可以通过关键字operate 重载操作符的含义. C++中支持重载的目的 诚然操作符的重载可以通过使用函数实现同样的功能 ...
- 重载操作符 operator overloading 学习笔记
重载操作符,只是另外一种调用函数的方法和表现方式,在某些情况它可以让代码更简单易读.注意不要过度使用重载操作符,除非它让你的类更简单,让你的代码更易读. 1语法 如下: 其中友元,关键字不是必须的,但 ...
- [019]转--C++ operator关键字(重载操作符)
原博客:http://www.cnblogs.com/speedmancs/archive/2011/06/09/2076873.html operator是C++的关键字,它和运算符一起使用,表示一 ...
- VC6.0中重载操作符函数无法访问类的私有成员
整理日: 2015年03月18日 在 C++ 中,操作符(运算符)可以被重载以改写其实际操作.同时我们可以定义一个函数为类的朋友函数(friend function)以便使得这个函数能够访问类的私有成 ...
- C++高精度运算类bign (重载操作符)
大数据操作,有例如以下问题: 计算:456789135612326542132123+14875231656511323132 456789135612326542132123*14875231656 ...
- C++ operator关键字(重载操作符)(转)
operator是C++的关键字,它和运算符一起使用,表示一个运算符函数,理解时应将operator=整体上视为一个函数名. 这是C++扩展运算符功能的方法,虽然样子古怪,但也可以理解:一方面要使运算 ...
- C++中operator关键字(重载操作符)
operator是C++的关键字,它和运算符一起使用,表示一个运算符函数,理解时应将operator=整体上视为一个函数名. 这是C++扩展运算符功能的方法,虽然样子古怪,但也可以理解:一方面要使运算 ...
- C++ Primer 学习笔记_60_重载操作符与转换 --赋值、下标、成员訪问操作符
重载操作符与转换 --赋值.下标.成员訪问操作符 一.赋值操作符 类赋值操作符接受类类型形參,通常该形參是对类类型的const引用,但也能够是类类型或对类类型的非const引用.假设未定义这个操作符, ...
随机推荐
- 用python写爬虫笔记(一)
https://bitbucket.org/wswp/code http://example.webscraping.com http://www.w3schools.com selenium.g ...
- JAVA中“==”和equals
A."=="可用于基本类型和引用类型: 当用于基本类型时候,是比较值是否相同:1==2: false: 当用于引用类型的时候,是比较是否指向同一个对象. B.基本类型int.c ...
- cpu中的缓存和操作系统中的缓存分别是什么?
cpu中的缓存和操作系统中的缓存分别是什么? 在操作系统中,为了提高系统的存取速度,在地址映射机制中增加了一个小容量的联想寄存器,即块表.用来存放当前访问最频繁的少数活动页面的页数.当某用户需要存取数 ...
- tkinter之事件绑定
- Android局域网访问webservice以及其中的一些问题
应老师的要求,要做个安卓app,实现备份app上的数据到服务器上的mongodb上,网上搜了下相关的实现方式.利用webservice技术,具体来说就是客户端直接调用服务器端的接口.之前从来没接触这玩 ...
- Codeforces 810 C. Do you want a date?
C. Do you want a date? time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- CF 1003C Intense Heat【前缀和/精度/双层暴力枚举】
The heat during the last few days has been really intense. Scientists from all over the Berland stud ...
- POJ2342 Anniversary party(动态规划)(树形DP)
Anniversary party Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6635 Accepted: 3827 ...
- SendMessage 和 PostMessage
1.首先是返回值意义的区别,我们先看一下 MSDN 里的声明: LRESULT SendMessage( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lPar ...
- [BZOJ2125]最短路(圆方树DP)
题意:仙人掌图最短路. 算法:圆方树DP,$O(n\log n+Q\log n)$ 首先建出仙人掌圆方树(与点双圆方树的区别在于直接连割边,也就是存在圆圆边),然后考虑点u-v的最短路径,显然就是:在 ...