栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++ 基础知识-类和对象-运算符重载2

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++ 基础知识-类和对象-运算符重载2

c++ 基础知识-类和对象-运算符重载2 1.赋值运算符重载
#include   
#include   
using namespace std;

class Int
{

public:
	Int(int i);//构造函数
	~Int();//析构函数
	int *m_Int;

	Int& operator=(Int &i)
	{
		//判断指针是否为空
		if (m_Int != NULL)
		{
			delete m_Int;
			m_Int = NULL;
		}
		//深拷贝
		m_Int = new int(*i.m_Int);
		return *this;
	}
};

Int::Int(int i)
{
	//深拷贝
	//开辟堆区
	m_Int = new int(i);//初始化
}

Int::~Int()
{
	//判断指针是否为空
	if (m_Int != NULL)
	{
		delete m_Int;
		m_Int = NULL;
	}
	cout<<"析构函数"<
    Int i1(10);
	cout<<*i1.m_Int<
	fun();
	//输出
	//10
	//10
	//10
	//析构函数
	//析构函数
	//析构函数
	return 0;
}  
2.关系运算符重载
#include   
#include   
using namespace std;

class Int
{

public:
	Int(int i);//构造函数
	~Int();//析构函数
	int m_int;
	bool operator==(Int &i)
	{
		if (this->m_int == i.m_int)
		{
			return true;
		} 
		return false;
	}
};

Int::Int(int i)
{
	m_int = i;
}

Int::~Int()
{
	cout<<"析构函数"<
	Int i1(23);
	Int i2(34);
	if(i1 == i2)
	{
		cout<<"Equal !!!"<
		cout<<"No equal !!!"<
	fun();
	return 0;
}  
3.函数调用运算符重载
#include   
#include   
using namespace std;

class Int
{

public:
	Int(int i);//构造函数
	~Int();//析构函数
	int m_int;
	//重载函数运算符号
	int operator()()
	{
		//cout<
	m_int = i;
}

Int::~Int()
{
	cout<<"析构函数"<
	Int i1(23);
	Int i2(34);
    cout<
	fun();
	return 0;
}  
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/923793.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号