C++ frequently asked questions

1. c++内置类型是否具有类的属性?

2. C++ 模板编程的掌握

3. C++有没有终极类?就是说不能作为超类的类

   ANSWER: 把所有构造函数都置为PRIVATE的,或者超类中含有PRIVATE 构造函数的

 

4. C++的类型转换操作符

作用:根据目的选择一个适合的操作符,而不是使用通用的类型转换。 指出了类型转换的原因,并让编译器能够检查程序的行为是否符合设计者的想法。

dynamic_cast:

     if Low is the super class of High

    Low *pl;

    High *ph;

     pl = dynamic_cast<Low*> ph;

 

const_cast:

删除const标签

High bar;

const High *pbar = &bar;

High *pb = const_cast<High *>(pbar); // remove the const from High *pbar

 

static_cast:

static_cast<type-name> (expression)

仅当type-name可被隐式转换为expression所属的类型或expression可被隐式转换为type-name所属的类型时,转换才是合法的。

e.g.

High is the super class of Low

HIgh bar;

Low blow;

High *pb = static_cast<High *>(&blow);  //valid uppercast

Low *pl = static_cast<Low *>(&bar); // valid downcast

枚举值可以被转换为整型,可以用static_cast 把整型转换为枚举值。可以使用static_cast将double转换为int,将float转换为long以及其他各种数值转换。

 

reinterpret_cast:

 

    

 

posted @ 2015-09-29 10:45  forwardslash  阅读(119)  评论(0)    收藏  举报