1.引用
点击查看代码
#include<stdio.h>
void xchang2(int* nu)
{
*nu = 8;
nu=122112;
}
void xchang(int& nu)
{
nu = 8;
}
int main()
{
int x = 3;
xchang(x);
printf("%d", x);
return 0;
}
1.引用类型来源于C++
2.引用类型相比于指针更加安全,引用类型无法修改指针的指向地址
3.使用引用类型可以像指针那样访问
4.只能赋值一次不能重新赋值
5.引用可以理解成是编译器维护的一个指针,
2.友元函数
点击查看代码
class MyClass
{
public:
friend void printf1(const MyClass& a);
private:
int x = 3;
int y = 4;
};
void printf1(const MyClass& a)
{
printf("%d --%d\n", a.x, a.y);
运算符重载
点击查看代码
class Number
{
private:
int lowValue ;
int highValue ;
public:
Number(int i,int q)
{
this->highValue = i;
this->lowValue = q;
}
Number operator+(const Number& a)
{
this->highValue = this->highValue + a.highValue;
this->lowValue = this->lowValue + a.lowValue;
return *this;
}
Number operator=(const Number& b)
{
this->highValue = b.highValue;
this->lowValue = b.lowValue;
return *this;
}
void prt()
{
printf("%d--%d", lowValue, highValue);
}
};
void Test()
{
Number p1(3,3), p2(4,4);
p1 = p2;
p1.prt();
}
运算符重载就是函数替换
. :: ?: sizeof # 不能重载