1. pointer和reference的区别
reference必须是要指向一个对象的,它不能指向null,并且reference是不能改变的,相当于门牌号是不变的,但是内容可以变。取地址用的是&
案例如下:
#include<iostream>
using namespace std;
void change(string &s){
string str = "123333333";
cout << &s << ' '<< s << endl;
s = str;
cout << &s << ' '<< s << endl;
}
int main(){
string s = "1111111111";
cout << &s << ' '<< s << endl;
change(s);
return 0;
}
/*
0x7fff7d8bb848 1111111111
0x7fff7d8bb848 1111111111
0x7fff7d8bb848 123333333
*/
pointer它是可以改变指向的,并且可以指向null,取内容用的是*
案例如下:
#include<iostream>
using namespace std;
void change(char *s){
char *str = "123333333";
cout << &s << ' '<< s << endl;
s = str;
cout << &s << ' '<< s << endl;
}
int main(){
char *s = "1111111111";
cout << &s << ' '<< s << endl;
change(s);
return 0;
}
/*
0x7ffffed132e8 1111111111
0x7ffffed132b8 1111111111
0x7ffffed132b8 123333333
*/
2. 尽量使用C++ 转型操作符
C语言会无条件直接强制转换,非常不安全。应该使用C++特有的转换方式:
1)static_cast<>: 使用的基本类型转换
2)const_cast<>:将常数性质的变量变成非常数性质,主要消除const的特性。
const_cast用来改变表达式中的常量性(constness)或变易性(volatileness)。使用const_cast,便是对人类(以及编译器)强调,通过这个转型操作符,我们唯一想改变的是某物的常量性或变易性。
假设将const_cast应用于上述以为的用途。那么转型动作会被拒绝。以下看一个样例:
class Widget{...};
class SpecialWidget:public Widget {...};
void update(SpecialWidget* psw);
SpecialWidget sw;//sw是个non-const对象
const SpecialWidget& csw = sw;//csw却是一个代表sw的reference。并视之为一个const对象
update(&csw);//错误!
//不能讲const SpecialWidget*传给一个须要SpecialWidget*的函数
update(const_cast<SpecialWidget*>(&csw));//可!
&csw的常量性被去除了。也因此,csw(亦即sw)在此函数中可被更改。
update((SpecialWidget*)&csw);//情况同上,但使用的是较难辨识的c旧式转型语法
Widget* pw = new SpecialWidget;
update(pw);//错误!pw的类型是Widget*,但update()须要的却是SpecialWidget*。
update(const_cast<SpecialWidgt*>(pw));//错误!const_cast仅仅能用来影响常量性或变易性,无法进行继承体系的向下转型动作。
3)dynamic_cast<>: 在继承上,安全的向下转型或者跨系转型动作
1)
int a = 12121;
double x = static_cast<double> (a);
2)
const int N = 1010;
int change(int *x){
int res = *x;
return res;
}
int main()
{
//int a = change((int *) (&N)); //C语言转换
int a = change(const_cast<int*> (&N)); //N的地址指向的值是一个常量值,最后变成可以改变的指针了
cout << a << endl;
return 0;
}
/*
*/