C++新增操作符(运算符):
new:分配内存空间
delete:释放内存空间
const_cast:
static_cast:
dynamic_cast:
reinterpret_cast:
this:
operator:
and, and_eq, bitand, bitor, compl, not, not_eq, or, or_eq, xor, xor_eq:各种运算符替代名
1.new和delete
int *ptr = new int;
*ptr = 10;
delete ptr;
ptr = NULL;
delete ptr;//if set ptr equalto NULL after delete ptr, delete ptr again will cause no problem, if not, that will cause double delete.
int *ptr2 = new int[10];
delete []ptr2;//[]-->表示批量删除,与new []保持一致
ptr2 = NULL;
2.const_cast、static_cast
const int i=10;
//int *p=&i; //error
//const-->const
//非const-->const
//const 不能-->非const
const int* ptr=&i;//1
int *p2=(int*)&i;//2
*p2=20;
cout<<"i:"<<i<<endl;
cout<<"*p2:"<<*p2<<endl;
//const_cast
int *p3=const_cast<int*>(&i);//3,解除const限定,只能对指针和引用
*p3=30;
cout<<"i:"<<i<<endl;
cout<<"*p3:"<<*p3<<endl;
//static_cast
int num1=5, num2=2;
//double ret=double(num1)/num2;
double ret=static_cast<double>(num1)/num2;//强制类型转换
cout<<"ret:"<<ret<<endl;