C++ type conversation
2008-03-25 14:52
666 查看
Type conversation
1. const_cast: convert between const and non-const
char *p1 = new char[6];
strcpy(p1,"Hello");
const char *p2 = p1;
char *p3 = const_cast< char *>(p2);
const char *p4 = const_cast< const char *>(p3);
p2[2] = 'U'; // it will report violate with a const variable
p3[2] = 'U'; // it's OK
p4[2] = 'U'; // it will report violate with a const variable
2. static_cast: C-stype type conversation
Any type conversions that the compiler performs implicitly can be made explicit through use of a
static_cast.
3. reinterpret_cast: it's used only between pointer type or pointer and integer type(becase int type is some like a pointer)
it will reinterpret bitmap of object.
A reinterpret_cast generally performs a low-level reinterpretation of the bit pattern of its operands,
and its correctness in large part depends on the active management of the programmer. For example, in the
following cast
4. dynamic_cast: used main between base class and inherit class
A dynamic_cast operator can be used to convert a pointer that refers to an object of class type to a
pointer to a class in the same class hierarchy. A dynamic_cast operator can also be used to convert an
lvalue for an object of class type to a reference to a class in the same class hierarchy. Unlike the other casts
supported in C++, a dynamic_cast is a cast that is performed at run-time. If the pointer or lvalue
operand cannot be cast to the target type of the conversion, the dynamic_cast fails. If a
dynamic_cast to a pointer type fails, the result of the dynamic_cast is the value 0. If a
dynamic_cast to a reference type fails, an exception is thrown.
-- dynamic_cast< Type* >( pe )
-- dynamic_cast< Type& >( lval )
class employee {
public:
virtual int salary();
};
class manager : public employee {
public:
int salary();
};
class programmer : public employee {
public:
int salary();
int bonus();
};
void payroll( employee *pe )
{
programmer *pm = dynamic_cast< programmer* >( pe );
if ( pm ) {
// to do
} else {
// to do
}
}
1. const_cast: convert between const and non-const
char *p1 = new char[6];
strcpy(p1,"Hello");
const char *p2 = p1;
char *p3 = const_cast< char *>(p2);
const char *p4 = const_cast< const char *>(p3);
p2[2] = 'U'; // it will report violate with a const variable
p3[2] = 'U'; // it's OK
p4[2] = 'U'; // it will report violate with a const variable
2. static_cast: C-stype type conversation
Any type conversions that the compiler performs implicitly can be made explicit through use of a
static_cast.
3. reinterpret_cast: it's used only between pointer type or pointer and integer type(becase int type is some like a pointer)
it will reinterpret bitmap of object.
A reinterpret_cast generally performs a low-level reinterpretation of the bit pattern of its operands,
and its correctness in large part depends on the active management of the programmer. For example, in the
following cast
4. dynamic_cast: used main between base class and inherit class
A dynamic_cast operator can be used to convert a pointer that refers to an object of class type to a
pointer to a class in the same class hierarchy. A dynamic_cast operator can also be used to convert an
lvalue for an object of class type to a reference to a class in the same class hierarchy. Unlike the other casts
supported in C++, a dynamic_cast is a cast that is performed at run-time. If the pointer or lvalue
operand cannot be cast to the target type of the conversion, the dynamic_cast fails. If a
dynamic_cast to a pointer type fails, the result of the dynamic_cast is the value 0. If a
dynamic_cast to a reference type fails, an exception is thrown.
-- dynamic_cast< Type* >( pe )
-- dynamic_cast< Type& >( lval )
class employee {
public:
virtual int salary();
};
class manager : public employee {
public:
int salary();
};
class programmer : public employee {
public:
int salary();
int bonus();
};
void payroll( employee *pe )
{
programmer *pm = dynamic_cast< programmer* >( pe );
if ( pm ) {
// to do
} else {
// to do
}
}
相关文章推荐
- [C++]Template Argument Deduction: automatic type conversation is not allowed
- C++中的type_info(中英文版)
- C++ Tutorial: Enhance Type Safety and Code Clarity with the nullptr Keyword
- C++ vector<int>::size_type的使用
- c++ cast type , static_cast and reinterpret_cast
- C++ type deduct summary
- C++编译出现binding ‘const string {aka const std::__cxx11::basic_string<char>}’ to reference of type ‘std
- 使用unordered_map的时候,出现C2338: The C++ Standard doesn't provide a hash for this type.
- error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
- C++中的类型擦除(type erasure in c++)
- C/C++ tip: How to detect the operating system type using compiler predefined macros
- c++编译错误unknown type name xxx
- 知无涯之C++ typename的起源与用法
- c++: size_type与 size_t一些概念
- error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 解决方法
- 关于C++程序编译出现error C2143: syntax error : missing ';' before 'type'的解决办法
- <Effective Mordern C++>笔记:Item 9:prefer alias declarations to typedefs.
- google-breakpad在 C++11下编译错误 ISO C++ forbids declaration of ‘typeof’ with no type
- C++ typedef typename 作用
- (C/C++)Windows Data Type