现在的位置: 首页 > 综合 > 正文

C++ cast

2013年02月11日 ⁄ 综合 ⁄ 共 1256字 ⁄ 字号 评论关闭

c++.static&const
1.static
static change the field and the life circle
a. variable with static in the file, this variable could only used in this file. 
b. variable with static in the function, the variable will exist till then program end rather then the end of the function.
c. variable with static in the class, this variable will belong to the class rather than the object.
2.const change the variable or the object

C++.cast
1.static_cast
static_case<type-id>(expression),
change expression to typeid, with no runtime security checking.
it can be used as fellow
a.cast between parent and child.
child to parent (secure), parent to child (insecure)
b.for basic data type cast.
int to char etc.
c.void to some other type.
d.other type to void
2.dynamic_cast
dynamic_cast<type-id>(expession)
the "type-id" and "expession" should both be pointer type or reference.
it could be used as follows:
a.cast between parent and child,just like static cast
but it will be safer for cast from "parent" to "child". 
b. cast between different type.
c. cross cast, not used very often.
3. reinpreter_cast
reinpreter_cast<type-id>(expession)
a.cast a pointer to an int
b.cast an int to a pointer
4.const_cast
const_cast<type-id>(expession)
change the const or volatile attribute.

volatile
a.volatile is a type specifier, for specifying the variable visited for different thread
b.tell the compile not the ommit it by compiling optimizing.
c.that's the program read the variable everytime visit it.

抱歉!评论已关闭.