相当于给变量取了别名,对其中任意变量的修改也会造成另一变量值的改变
用法:
加const
成常引用变量时,不可对通过引用变量更改引用值(常指针变量与之类似)
用于函数形参
int &getn(){return n;}
int main(){
getn()=5;
output(n);
return 0;
}
int *p=new int;
delete p;
p=new int[10]; //Assign space of the int array for p
delete [] p; //Delete the array
malloc/free
的差异:
new
能够创建对象,会调用构造函数,而malloc
只是单纯分配了一块空间delete
删除对象时调用析构函数,保证类内申请的动态内存能通过析构函数一并释放,而free
只是单纯释放指针所指空间delete
&delete []
的差异:
=
赋值,但不能直接比较大小class A{
int x;
public:
void value(int n=0){x=n;}
int value(){return x;}
} //当调用value()时,存在二义性
原文:https://www.cnblogs.com/DreamEagle/p/12631944.html