码迷,mamicode.com
首页 > 编程语言 > 详细

c++相关知识回顾

时间:2015-05-05 23:33:53      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:

1.typedef

typedef用来定义同类型的同义词。如:

1 typedef unsingned int size_t;
2 typedef int ptrdiff_t;
3 typedef T * iterator;

需要注意的是,不应该将typedef当做文本扩展来看待,如下例:

1 typedef string * pstring;
2 const pstrng cstr;

此时第二行中cstr应该表示什么类型呢?若当做文本扩展,则会认为cstr表示const string *,即cstr是一个指针,指向一个const string对象。但是实际上,const修饰的是pstring类型,而pstring是一个string类型指针,则cstr应该表示的是一个指向string类型对象的const指针。

 

2.sizeof操作符

sizeof操作符的作用是返回一个对象或类型名的长度,返回值的类型为size_t,长度的单位为字节。

 1 //sizeof 操作符的三种用法
 2 sizeof(type name);
 3 sizeof(expr);
 4 sizeof expr;
 5 //eg.
 6 string ss,*sptr;
 7 int i;
 8 sizeof(ss);
 9 sizeof(string);
10 sizeof *sptr;
11 sizeof(i);
12 sizeof (int);

可以用sizeof操作符求一个数组的元素个数,如下:

1 int a[10];
2 int sz = sizeof(a)/sizeof(int);//sz == 10

 

3.显示类型转换(强制类型转换)

命名的强制类型转换符号一般形式如下:

1 cast-name<type>(expression);

a.dynamic_cast

dynamic_cast操作符将基类类型对象的引用或指针转换为同一继承层次中其他类型的引用或指针。

dynamic_cast与其他强制转换的不同之处在于,dynamic_cast涉及运行时类型检查。如果绑定到引用或指针的对象不是目标类型的对象,则dynamic_cast失败。如果转换到指针类型的dynamic_cast失败,则dynamic_cast的结果为0值;如果转换到引用类型的dynamic_cast失败,则抛出一个bad_cast异常。看一个例子:

 1 class Base{
 2 ...
 3 };
 4 
 5 class Derived:public Base{
 6 ...
 7 };
 8 
 9 //Base *baseptr = new Base;
10 Base *baseptr = new Derived;
11 if (Derived *derivedPtr = dynamic_cast<Derived *>(baseptr)){
12 ...
13 }else{
14 ...
15 }

如果baseptr实际指向Derived对象,则转换成功;若baseptr指向Base对象,则转换失败。

b.const_cast

const_cast可以转换掉表达式中的const性质。举例如下:

1 void process(string *sptr);
2 const string *csptr;
3 process(const_cast<string *>(cspttr));

函数process接受string*参数,若传给其一个const string *是不可以的,使用const_cast便可将const string*转换为string*。

b.static_cast

编译器隐式执行的任何类型准换都可由static_cast显式完成。

1 double dval = 3.14;
2 char cval = static_cast<char>(dval);

还可用static_cast找回存放在void *指针中的值。

1 double dval = 3.14;
2 void * ptr = &dval;
3 double *dp = static_cast<double *>(ptr);

 

4.c++中的三种new

a.new表达式

new表达式动态创建对象,返回指向新创建对象的指针,该对象创建于自由存储区。用new表达式创建的对象内存必须用delete表达式释放,如果忘记释放该对象,则就会造成内存泄露。当new表达式无法获取所需内存空间时,系统将抛出bad_alloc异常。

1 int *pi = new int(10);
2 int *pj = new int;
3 string *ps = new string("1024");
4 string *psj = new string;

当使用new表达式的时候,实际发生了三个步骤。首先,表达式调用operator new()操作符函数分配足够大的未初始化内存,以保存指定类型的一个对象;接下来,运行该类型的一个构造函数,用指定初始化式在未初始化内存上构造对象;最后,返回指向新分配并构造的对象的指针。

b.operator new和operator delete

operator new用来获得未初始化的内存,它类似于allocator类中的allocate成员函数;operator delete用来释放内存,它类似于allocator类中的deallocate()成员函数。operator delete不会运行析构函数,因此在调用operator delete前应显示调用析构函数清除对象本身。operator new与operator delete的原型如下:

 1 void *operator new(size_t);
 2 void *operator new[](size_t);
 3 void *operator delete(void *);
 4 void *operator delete[](void *);
 5 
 6 //eg.
 7 string *ps = static_cast<string *>(operator new(sizeof(string)));
 8 int sz = 10;
 9 int *pi = static_cast<int *>(operator new(sizeof(int)*sz));
10 operator delete[](pi);

c.定位new(placement new)

定位new表达式在已分配的原始内存中初始化一个对象。它与operator new的不同之处在于它不分配内存额对象,相反,它接受指向已分配但未构造的内存的指针,并在该内存中初始化一个对象。这与allocator类中的construct成员函数类似。定位new表达式形式如下:

1 new (place_address) type;
2 new (place_address) type(initializer_list);
3 
4 //eg.
5 string *ps = static_cast<string *>(operator new(sizeof(string)));
6 new (ps) string("hello world");

 

c++相关知识回顾

标签:

原文地址:http://www.cnblogs.com/lxiao/p/4471255.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!