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 # 不能重載