C++引用、友元函数、运算符重载

  1. 引用和指针在底层汇编阶段没有任何区别,都是地址传递。
  2. 不同:指针可能出现地址乱指的问题,而引用则不会,改变引用的值实际上是改变变量地址上的值,而不是变量的地址。
  3. 引用是C++中特有类型。
  4. 引用类型只能赋值一次,不能重新赋值。
  5. 引用只是变量的一个别名。
  6. 引用可以理解成是编译器维护的一个指针,但并不占用空间。
  7. 使用引用可以像指针那样去访问、修改对象的内容,但更安全。
void Printf(Base & a,Base *ap)
{
	int temp = 10;
	&a = &temp;//错误,引用不能重新赋值
	a = 10;//正确,改变这个地址上变量的值。
	ap = &temp;//正确,指针可以重新赋值,只是意义已经不同。
}

友元函数:

  1. 就是给这个函数访问私有成员的权限。
  2. 运算符重载的某些场合需要使用友元。
  3. 两个类要共享数据的时候。

友元函数和类的成员函数的区别:

  1. 成员函数有this指针,而友元函数没有this指针。
  2. 友元函数是不能被继承的,就像父亲的朋友未必是儿子的朋友。
#include<stdio.h>

class Student
{
private:
	int x;
	int y;
public:
	Student(int x,int y)
	{
		this->x = x;
		this->y = y;
	}
	friend void Printf1(Student* p);
	friend void Printf2(const Student& p);
};
//但是现在都不能访问 想要访问私有成员,必须在类声明中声明这个函数是我这个类的朋友
void Printf1(Student* p)
{
	printf("%d   %d   \n",p->x,p->y);
}

void Printf2(const Student& p) //加const是为了避免意外修改变量的值,但是现在仍然不能访问私有成员
{
	printf("%d   %d   \n",p.x,p.y);
}

int main(int argc,char *argv[])
{
		Student s(1,2);
		Printf1(&s);
		Printf2(s);
		return 0;
}

运算符重载

  1. 就是函数替换。
  2.  .   ::   ?:   sizeof   #  不能重载外,其余的都是可以重载的。
  3. 经常需要重载的运算符,就是 >   <   == 。
#include<stdio.h>
class Number
{
private:
	int lowValue;
	int highValue;
public:
	Number(int lowValue,int highValue)
	{
		this->lowValue = lowValue;
		this->highValue = highValue;
	}
	void Print();
	//void Plus();//plus已经实现了int ++的功能,但是每次都要调用这个函数,怎么做到和普通++功能意义,而不需要自己再写这个函数呢.
	Number operator++();//int++时的返回值是int,所有结构体/类的返回值必须是对应的类型。
	Number operator+(const Number& p);
	friend bool operator>(const Number& p1,const Number& p2);
	friend bool operator<(const Number& p1,const Number& p2);
	friend bool operator==(const Number& p1,const Number& p2);
};
void Number::Print()
{
	printf("low = %d ,  high = %d\n",lowValue,highValue);
}
/*
void Number::Plus()
{
	lowValue++;
	highValue++;
}
*/
Number Number::operator++()
{
	lowValue++;
	highValue++;
	return *this;
}

Number Number::operator+(const Number& p)
{
	this->lowValue += p.lowValue;
	this->highValue += p.highValue;
	return *this;
}


bool operator>(const Number& p1,const Number& p2)
{
	if((p1.lowValue > p2.lowValue) && (p1.highValue > p2.highValue))
		return true;
	else
		return false;
}
bool operator<(const Number& p1,const Number& p2)
{
	if((p1.lowValue < p2.lowValue) && (p1.highValue < p2.highValue))
		return true;
	else
		return false;
}
bool operator==(const Number& p1,const Number& p2)
{
	if((p1.lowValue == p2.lowValue) && (p1.highValue == p2.highValue))
		return true;
	else
		return false;
}


int main(int argc,char *argv[])
{
		Number n(1,2);
		n.Print();
	//	n.Plus();//实现了++的功能,给它起个别名可以吗?
//		n.Print();
		n++;
		n.Print();

		Number n1(2,3);
		n = n+n1;
		n.Print();

		if(n>n1)
			printf(">  true\n");
		else
			printf(">  false\n");

		if(n<n1)
			printf("<  false\n");
		else
			printf("<  true\n");
		if(n == n1)
			printf("==\n");
		else
			printf("!=\n");


		return 0;
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C++中的运算符重载可以通过友元函数来实现。友元函数是定义在类外部的普通函数,但是可以访问类的所有私有成员。通过将运算符重载函数声明为类的友元函数,可以使该函数访问类的私有成员。 下面是一个示例代码,演示了如何通过友元函数实现运算符重载: ```c++ #include <iostream> class Complex { public: Complex(double real = 0.0, double imag = 0.0) : real_(real), imag_(imag) {} double real() const { return real_; } double imag() const { return imag_; } private: double real_; double imag_; friend Complex operator+(const Complex& c1, const Complex& c2); }; Complex operator+(const Complex& c1, const Complex& c2) { return Complex(c1.real_ + c2.real_, c1.imag_ + c2.imag_); } int main() { Complex c1(1.0, 2.0); Complex c2(3.0, 4.0); Complex c3 = c1 + c2; std::cout << "real: " << c3.real() << ", imag: " << c3.imag() << std::endl; return 0; } ``` 在上面的代码中,我们定义了一个Complex类,包含了两个私有成员变量real_和imag_,表示复数的实部和虚部。我们通过友元函数operator+来实现运算符重载,使得两个Complex对象可以通过"+"运算符相加。在友元函数operator+中,我们可以访问Complex类的私有成员变量real_和imag_,从而实现了运算符重载。 在main函数中,我们创建了两个Complex对象c1和c2,并通过运算符重载将它们相加,最终得到一个新的Complex对象c3。我们可以通过调用c3的real()和imag()方法来获取它的实部和虚部。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

雲烟

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

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

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

打赏作者

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

抵扣说明:

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

余额充值