Class Type class b{ public: int m_num; b (): M_num ({}}; ) vOid foo (void) { const b* b1 = new b (); B* b2 = const_cast<B*> (B1); b2->m_num = 200; cout << "B1:" << b1->m_num << endl;// 200 cout << "B2:" << b2->m_num << endl;// 200 const b b3; b b4 = const_cast<b &> (b3); b4.m_num = 300; cout << "B3:" << b3.m_num << endl;//50 cout << "B4:" << b4.m_num << endl;//300}//************************//
Basic Type Void foo () { const int a = 100; int* p1 = const_cast<int*> (&a); *p1 = 200; cout << *p1 << endl;//200 cout << a << endl;//100 const int* p2 = new int (; int* p3 = const_cast<int*>) (p2); *p3 = 200; cout << *p2 < < endl;//200 cout << *p3 << endl;//200}//************************//
You will find:
A: Can be a basic type or class type;
const a A; Whatever you want to change a.
A const a * p = new A (); After removing the Const property of P, the *p changes.
class a{ public: a () { m_num=1; } int m_num;}; void foo (void) { a a; const a &r = a; A a1 = const_cast<A&> (a); a1.m_num = 200; cout << a1.m_num << endl;//200 cout << a.m_num << endl;//1}//****************//
const_cast<type-id> (expression), Type-id can only be a pointer or reference, the other is wrong, the expression can be removed
The Const property or the Volatil property in expression can also increase the Const property or the Volatil property
const int i = 10;
int i1 = const_cast<int> (i)//error
Adding the Const property is the opposite of the Volatil property.
This article is from the "12208412" blog, please be sure to keep this source http://12218412.blog.51cto.com/12208412/1867169
const_cast<type-id> (expression)