【C++】类和对象(中)

一、类的六个默认成员函数

如果一个类中什么成员都没有,简称为空类

其实在空类中并不是什么都没有,任何类在什么都不写时,编译器会自动生成以下6个默认成员函数。
默认成员函数: 用户没有显式实现,编译器会自动生成的成员函数称为默认成员函数。
在这里插入图片描述

二、构造函数

2.1、什么是构造函数

当我们在使用类来初始化一个对象时,通常都需要调用他的Init公有方法给对象进行初始化。未免有点麻烦,那能否在对象创建时,就将信息设置进去呢?

构造函数是一个特殊的成员函数,名字与类名相同,创建类类型对象时由编译器自动调用,以保证每个数据成员都有 一个合适的初始值,并且在对象整个生命周期内只调用一次。

构造函数有如下特性:

  • 函数名与类名相同。
  • 无返回值。
  • 对象实例化时编译器自动调用对应的构造函数。
  • 构造函数可以重载。
  • 如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一旦用户显式定义编译器将不再生成。
  • 构造函数对内置类型不做处理,对于自定义类型会调用他的默认构造函数。
  • 无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。注意:无参构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为是默认构造函数。
class Date
{
public:
	void Init(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1;
	d1.Init(2022, 7, 5);
	d1.Print();
	Date d2;
	d2.Init(2022, 7, 6);
	d2.Print();
	return 0;
}

对于Date类,可以通过 Init 公有方法给对象设置日期,也可以调用他的构造函数来构造对象
在这里插入图片描述

我们可以看到编译器帮助我们自动调用了构造函数,而构造函数的功能就是帮助我们初始化对象,下面我们来看一下构造函数的几大特性。

2.2 构造函数的特性

构造函数是特殊的成员函数,需要注意的是,构造函数虽然名称叫构造,但是构造函数的主要任
务并不是开空间创建对象,而是初始化对象

构造函数的特征如下:

  1. 函数名与类名相同。
  2. 无返回值。
  3. 对象实例化时编译器自动调用对应的构造函数。
  4. 构造函数可以重载。
  5. 如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一旦 用户显式定义编译器将不再生成
class Date
{
public:
	
	 如果用户显式定义了构造函数,编译器将不再生成
	//Date(int year, int month, int day)
	//{
	//_year = year;
	//_month = month;
	//_day = day;
	//}
	
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{

	Date d1;		//屏蔽有参构造参数  编译器自动生成无参构造
	//Date d2(2022, 5, 20);		
	类内有有参构造参数,编译器不再自动生成无参构造,
	 调用走有参构造 无参调用失败  
	可以显示写无参构造,这样有参无参均可调用
	return 0;
}

关于编译器生成的默认成员函数,很多人会有疑惑:不实现构造函数的情况下,编译器会
生成默认的构造函数。但是看起来默认构造函数又没什么用?d对象调用了编译器生成的默
认构造函数,但是d对象_year/_month/_day,依旧是随机值。也就说在这里编译器生成的 默认构造函数并没有什么用??
解答:C++把类型分成内置类型(基本类型)和自定义类型。内置类型就是语言提供的数据类
型,如:int/char…,自定义类型就是我们使用class/struct/union等自己定义的类型,看看
下面的程序,就会发现编译器生成默认的构造函数会对自定类型成员_t调用的它的默认成员 函数。

class Time
{
public:
	Time()
	{
		cout << "Time()" << endl;	//打印看是否自动调用Time()
		_hour = 0;
		_minute = 0;		
		_second = 0;
	}
private:
	int _hour;
	int _minute;
	int _second;
};
class Date
{
private:
	// 基本类型(内置类型)
	int _year=2022;
	int _month=3;	//这里使用了特性六,在类的声明中给默认值
	int _day=15;
	// 自定义类型
	Time _t;
};
int main()
{
	Date d;
	return 0;
}

运行上面的代码,我们可以发现Data类构造对象的时候自动调用了Time的构造函数。

  1. 内置类型成员变量在类中声明时可以给默认值。
  2. 无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。注意:无参构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为是默认构造函数
class Date
{
public:
	//Date()
	//{
	//	_year = 1900;
	//	_month = 1;
	//	_day = 1;
	//}
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1;
	Date d2(2022, 9, 25);
	d2.Print();
	d1.Print();
}

三、析构函数

3.1、什么是析构函数

一个对象是由构造函数初始化的,那么是怎么没的呢?与构造函数功能相反,析构函数不是完成对对象本身的销毁,局部对象销毁工作是由编译器完成的。而对象在销毁时会自动调用析构函数,完成对象中资源的清理工作。

3.2、 析构函数的特性

析构函数是特殊的成员函数,其特征如下:

  1. 析构函数名是在类名前加上字符 ~。
  2. 无参数无返回值类型。
  3. 一个类只能有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。注意:析构
    函数不能重载
  4. 对象生命周期结束时,C++编译系统系统自动调用析构函数
  5. 关于编译器自动生成的析构函数,是否会完成一些事情呢?下面的程序我们会看到,编译器
    生成的默认析构函数,对自定类型成员调用它的析构函数。
class Time
{
public:
	Time()
	{
		cout << "Time()" << endl;	//打印看是否自动调用Time()
		_hour = 0;
		_minute = 0;		
		_second = 0;
	}
	~Time()
	{
		cout << "~Time()" << endl;
	}
private:
	int _hour;
	int _minute;
	int _second;
};

class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
	~Date()
	{
		cout << "~Date()" << endl;
	}
private:
	int _year;
	int _month;
	int _day;
	Time _time;
};
int main()
{
	Date d1;
	Date d2(2022, 9, 25);
	d2.Print();
	d1.Print();
	//结束后自动调用析构,同时调用TIme的析构
}

在这里插入图片描述
6. 如果类中没有申请资源时,析构函数可以不写,直接使用编译器生成的默认析构函数,比如Date类;有资源申请时,一定要写,否则会造成资源泄漏,比如Stack类。

四、拷贝构造函数

4.1、什么是拷贝构造

在创建对象时,我们可以创建一个与已存在的对象一摸一样的新对象,那么这个功能就由我们的拷贝构造函数来完成。

拷贝构造函数:只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存 在的类类型对象创建新对象时由编译器自动调用。

class Time
{
public:
	Time()
	{
		cout << "Time()" << endl;	//打印看是否自动调用Time()
		_hour = 0;
		_minute = 0;		
		_second = 0;
	}
	~Time()
	{
		cout << "~Time()" << endl;
	}
private:
	int _hour;
	int _minute;
	int _second;
};

class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	Date(const Date&a)
	{
		_year = a._year;
		_month = a._month;
		_day = a._day;
		_time = a._time;
	}
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
	~Date()
	{
		cout << "~Date()" << endl;
	}
private:
	int _year;
	int _month;
	int _day;
	Time _time;
};
int main()
{
	Date d1(2022, 9, 25);
	Date d2(d1);	//调用拷贝构造函数
	d1.Print();
	d2.Print();

}

4.2、拷贝构造函数的特性

拷贝构造函数也是特殊的成员函数,其特征如下:

  1. 拷贝构造函数是构造函数的一个重载形式
  2. 拷贝构造函数的参数只有一个且必须是类类型对象的引用,使用传值方式编译器直接报错,因为会引发无穷递归调用。
  3. 若未显示定义,编译器会生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝叫做浅拷贝,或者值拷贝。
  4. 默认的拷贝构造函数对内置类型以字节为单位直接进行拷贝(浅拷贝),对自定义类型调用其自身的拷贝构造函数。

在这里插入图片描述
拷贝构造函数的参数只有一个且必须是类类型对象的引用,使用传值方式编译器直接报错,因为会引发无穷递归调用。 为什么呢?

传值会引发无限递归!!!

在这里插入图片描述
所以我们必须使用引用来做形参,这样的话在调用拷贝构造的时候,形参就是实参自己,那么就不需要再拷贝一份实参,拷贝构造函数直接就可以完成对待初始化对象的初始化。当然,我们在写拷贝构造函数的时候,一般都在前面加const,这样就可以避免对原对象的修改。

下面来说一下什么是深拷贝 什么是浅拷贝!

浅拷贝就是值拷贝,就是你有什么我拷贝什么,不管结果如何
深拷贝就是浅拷贝的加强,考虑到有些资源具有唯一性,比如类,你拷贝了我的堆资源,我释放后,正常来说就结束了
结果你又释放一次,这就是浅拷贝的危害,深拷贝就是在值拷贝的基础上,像拷贝对象意义拷贝一份新的资源,而不是旧有的!!

在这里插入图片描述
此时默认拷贝构造函数的弊端就出现了,就必须我们手写拷贝构造,给构造出来的对象开辟资源。

typedef int DataType;
class Stack {
public:
	//构造函数
	Stack()
	{
		cout << "构造函数Stack()" << endl;
		_array = (DataType*)malloc(sizeof(DataType) * 3);
		if (!_array)
		{
			perror("malloc fail::");
			exit(-1);
		}
		_capacity = 3;
		_top = 0;
	}
	//...
	
	//析构函数
	~Stack()
	{
		cout << "析构函数~Stack()" << endl;
		if (_array)
		{
			free(_array);
			_array = nullptr;
		}
		_top = _capacity = 0;
	}
private:
	DataType* _array;
	int _top;
	int _capacity;
};
int main()
{
	Stack st1;
	Stack st2(st1);
	return 0;
}

在这里插入图片描述
在这里插入图片描述
当编译器自动生成的默认拷贝构造函数进行初始化栈时,我们发现两个对象中的_array指针竟然指向了同一块空间。这是因为编译器默认生成的拷贝构造函数拷贝数据时,是按照byte进行拷贝的,那么拷贝的时候自然也会将st1的地址拷贝给st2,这就导致了st2和st1指向了同一块空间。当主函数结束的时候,需要调用析构函数来销毁两个对象,但是由于两个对象中的_array都指向了同一块空间,所以导致了同一块空间被释放了两次。

这种情况不仅析构两次有问题,而且在插入数据时,一个对象的改变必然会覆盖另一个对象的数据,所以这种浅拷贝的问题会造成严重的后果,那么我们应该如何避免这种情况的发生呢?这个时候我们就得写一个深拷贝拷贝构造函数了。

Stack(const Stack& s)
	{
		_array = (DataType*)malloc(sizeof(DataType) * s._capacity);
		if (!_array)
		{
			perror("malloc fail::");
			exit(-1);
		}
		_top = s._top;
		_capacity = s._capacity;
	}

所以我们什么时候需要写拷贝构造函数呢?很简单,如果类中有资源申请我们就必须手动实现拷贝构造函数。如果没有的话那就直接使用类提供的默认拷贝构造即可。

拷贝构造函数典型调用场景:

  • 使用已存在对象创建新对象
  • 函数参数类型为类类型对象
  • 函数返回值类型为类类型对象

五、赋值运算符重载

5.1、运算符重载

C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。

函数名字:关键字operator后面接需要重载的运算符符号。
函数原型:返回值类型 operator操作符(参数列表)

对于运算符的重载我们需要注意几点:

  1. 不能通过连接其他符号来创建新的操作符:比如operator@
  2. 重载操作符必须有一个类类型参数
  3. 用于内置类型的运算符,其含义不能改变,例如:内置的整型+,不能改变其含义
  4. 作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this
  5. .* :: sizeof ?: . 注意以上5个运算符不能重载。这个经常在笔试选择题中出现。

具体使用: 类内成员函数

class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	 //bool operator==(Date* this, const Date& d2)
	// 这里需要注意的是,左操作数是this,指向调用函数的对象
	bool operator==(const Date & d2)
	{
		return _year == d2._year
		&& _month == d2._month
			&& _day == d2._day;
	}
private:
	int _year;
	int _month;
	int _day;
};
void Test()
{
	Date d1(2018, 9, 26);
	Date d2(2018, 9, 27);
	cout << d1.operator==(d2)<< endl;
}
int main()
{
	Test();
}

在这里插入图片描述

也可以类外定义 但需要传入两个参数 类内传一个参数是因为类内成员默认有一个参数this指针,指向对象自身,所以传一个

// 全局的operator==
class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	//private:
	int _year;
	int _month;
	int _day;
};
// 这里会发现运算符重载成全局的就需要成员变量是公有的,那么问题来了,封装性如何保证?
// 这里其实可以用我们后面学习的友元解决,或者干脆重载成成员函数。
bool operator==(const Date& d1, const Date& d2)
{
	return d1._year == d2._year
		&& d1._month == d2._month
		&& d1._day == d2._day;
}



void Test()
{
	Date d1(2018, 9, 26);
	Date d2(2018, 9, 27);
	cout << operator==(d1,d2)<< endl;
}
int main()
{
	Test();
}		//运行结果自然也是0
//由于是类外的函数,必须设置成员为public 或者 设置友元函数 本例为设置public

上面的是设置属性为public

也可以利用关键字friend来设置友元函数可以增加封装性:
在这里插入图片描述

5.2、赋值运算符重载

1. 赋值运算符重载格式

  • 参数类型:const T&,传递引用可以提高传参效率
  • 返回值类型:T&,返回引用可以提高返回的效率,有返回值目的是为了支持连续赋值
  • 检测是否自己给自己赋值
  • 返回*this :要复合连续赋值的含义
class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	Date& operator=(const Date& d)
	{
		if (this != &d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}
	void Print()
	{
		cout << _year << " " << _month << " " << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1(2022, 10, 4);
	Date d2 = d1;
	d1.Print();
	d2.Print();

}

在这里插入图片描述

2. 赋值运算符只能重载成类的成员函数不能重载成全局函数

原因:赋值运算符如果不显式实现,编译器会生成一个默认的。此时用户再在类外自己实现
一个全局的赋值运算符重载,就和编译器在类中生成的默认赋值运算符重载冲突了,故赋值
运算符重载只能是类的成员函数

在这里插入图片描述

3. 用户没有显式实现时,编译器会生成一个默认赋值运算符重载,以值的方式逐字节拷贝。

注意:内置类型成员变量是直接赋值的,而自定义类型成员变量需要调用对应类的赋值运算符
重载完成赋值。

编译器既然已经实现了那我们还有必要自己实现吗?
在这里插入图片描述

有必要,如果是一般的类自然自动生成的可以实现,但是如果类内管理者堆中的资源的话,必须自己实现,以便于完成资源的赋值,而不是指向同一块空间,导致程序崩溃。

5.3、前置++运算符和后置++运算符重载

	// 前置++:返回+1之后的结果
// 注意:this指向的对象函数结束后不会销毁,故以引用方式返回提高效率
	Date& operator++()
	{
		_day += 1;
		return *this;
	}
	// 后置++:
	// 前置++和后置++都是一元运算符,为了让前置++与后置++形成能正确重载
	// C++规定:后置++重载时多增加一个int类型的参数,但调用函数时该参数不用传递,编译器动传递
		// 注意:后置++是先使用后+1,因此需要返回+1之前的旧值,故需在实现时需要先将this保存份,然后给this + 1
		// 而temp是临时对象,因此只能以值的方式返回,不能返回引用
		Date operator++(int)
	{
		Date temp(*this);
		_day += 1;
		return temp;
	}

在这里插入图片描述
注意

案例始终有所瑕疵,并不规范,比如传入 2022 14 55,代码仍会执行,但现实中并不存在这样的日期,仅供参考!!!

六、日期类完整代码

以下为完整代码:

Date.h

#pragma once
#pragma once
#include<iostream>
using namespace std;
#include<cassert>

class Date
{
public:
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);

	//打印函数
	void Print()const;

	//构造函数
	Date(int year = 1, int month = 1, int day = 1);

	//获取某个月的天数
	int GetMonthDay(int year, int month);

	//重载==运算符
	bool operator==(const Date& d)const;

	//重载!=运算符
	bool operator!=(const Date& d)const;

	//重载<运算符
	bool operator<(const Date& d)const;

	//重载<=运算符
	bool operator<=(const Date& d)const;

	//重载>运算符
	bool operator>(const Date& d)const;

	//重载>=运算符
	bool operator>=(const Date& d)const;

	//重载+=运算符
	Date& operator+=(int d);

	//重载+运算符
	Date operator+(int d)const;

	//重载-=运算符
	Date& operator-=(int d);

	//重载-运算符
	Date operator-(int d)const;

	//重载d1-d2运算符
	int operator-(const Date& d)const;

	//重载前置++
	Date& operator++();

	//重载后置++
	Date operator++(int);

	//重载前置--
	Date& operator--();

	//重载后置--
	Date operator--(int);
	//将一年中的第几天转换成日期
	void DaysToDate();
	//计算日期是一年中的第几天
	int DayOfYear() const;

private:
	int _year;
	int _month;
	int _day;
};

//重载左移运算符
inline ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}

inline istream& operator>>(istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}

Date.cpp

#define _CRT_SECURE_NO_WARNINGS
#include"Date.h"

//构造函数
Date::Date(int year, int month, int day)
{
	assert(month < 13 && day <= GetMonthDay(year, month));
	_year = year;
	_month = month;
	_day = day;
}

//获取某个月的天数
int Date::GetMonthDay(int year, int month)
{
	assert(month > 0 && month < 13);
	int arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (month == 2 && (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)))
	{
		return 29;
	}
	return arr[month];
}

//打印函数
void Date::Print()const
{
	cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

//重载==运算符
bool Date::operator==(const Date& d)const
{
	return (_year == d._year && _month == d._month && _day == d._day);
}

//重载!=运算符
bool Date::operator!=(const Date& d)const
{
	return !(*this == d);
}

//重载<运算符
bool Date::operator<(const Date& d)const
{
	return (_year < d._year)
		|| ((_year == d._year) && (_month < d._month))
		|| ((_year == d._year) && (_month == d._month) && (_day < d._day));
}

//重载<=运算符
bool Date::operator<=(const Date& d)const
{
	return (*this == d) || (*this < d);
}

//重载>运算符
bool Date::operator>(const Date& d)const
{
	return !(*this <= d);
}

//重载>=运算符
bool Date::operator>=(const Date& d)const
{
	return !(*this < d);
}

//重载+=运算符
Date& Date::operator+=(int d)
{
	if (d < 0)
	{
		return *this -= -d;
	}
	_day += d;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month > 12)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}

//重载+运算符
Date Date::operator+(int d)const
{
	Date tmp(*this);
	tmp += d;
	return tmp;
}

//重载-=运算符
Date& Date::operator-=(int d)
{
	if (d < 0)
	{
		return *this += -d;
	}
	_day -= d;
	while (_day <= 0)
	{
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}

//重载-运算符
Date Date::operator-(int d)const
{
	Date tmp(*this);
	tmp -= d;
	return tmp;
}

//重载d1-d2运算符
int Date::operator-(const Date& d)const
{
	Date max = *this;
	Date min = d;
	int flag = 1;
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int cout = 0;
	while (min != max)
	{
		++min;
		++cout;
	}
	return cout * flag;
}

//重载前置++
Date& Date::operator++()
{
	*this += 1;
	return *this;
}

//重载后置++
Date Date::operator++(int)
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}

//重载前置--
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}

//重载后置--
Date Date::operator--(int)
{
	Date tmp(*this);
	*this -= 1;
	return tmp;
}
//将一年中的第几天转换成日期
void Date::DaysToDate()
{
	int year = 0;
	int days = 0;
	cin >> year >> days;

	static int Day[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	if (((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
		&& (days > 60))
	{
		--days;
	}

	int month = 1;
	while (days > Day[month])
	{
		days -= Day[month];
		month++;
	}

	printf("%04d-%02d-%02d\n", year, month, days);
}

//计算日期是一年中的第几天
int Date::DayOfYear() const
{
	static int Day[13] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };

	int n = Day[_month - 1] + _day;

	if (_month > 2 && ((_year % 4 == 0 && _year % 100 != 0) || (_year % 400 == 0)))
		++n;

	return n;
}


int main()
{
	Date d1(2022, 10, 2);
	d1.Print();
	cout << endl;
	cout <<"是当年的"<< d1.DayOfYear() << "天" << endl;

	d1.DaysToDate();

}

在这里插入图片描述

七、const成员函数

const修饰的“成员函数”称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改

在这里插入图片描述

const对象只能调用const成员函数
非const对象既可以调用const成员函数 也可以调用非const 成员函数

八、取地址及const取地址操作符重载

取地址操作符重载也属于我们类的六个默认成员函数之一,所以平常当我们访问对象的地址时,类也会给我们提供对应的的默认成员函数,所以一般情况下我们都不需要对取地址操作符重载。

class A
{
public:
	void Print() const
	{
		cout << _a << endl;
	}
	A* operator&()
	{
		return this;
	}
	const A* operator&() const
	{
		return this;
	}

private:
	int _a = 10;
};

这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需
要重载,比如想让别人获取到指定的内容!

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

翻转的乌龟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值