const_cast<type-id>(expression)
//**********************// 类类型 class B{ public: int m_num; B():m_num(50){} }; void foo(void) { const B* b1 = new B(); B* b2 = const_cast(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(b3); b4.m_num = 300; cout << "b3:" << b3.m_num << endl;//50 cout << "b4:" << b4.m_num << endl;//300 } //************************//
//************************// 基本类型 void foo(){ const int a = 100; int* p1 = const_cast(&a); *p1 = 200; cout << *p1 << endl;//200 cout << a << endl;//100 const int* p2 = new int(100); int* p3 = const_cast (p2); *p3 = 200; cout << *p2 << endl;//200 cout << *p3 << endl;//200 } //************************//
你会发现:
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名注册、网络空间、营销软件、网站建设、桃山网站维护、网站推广。
A:可以为基本类型或者类类型;
const A a;随便怎么修改a都不会变化
const A* p = new A();去掉p的const属性后,*p就变化了.
//*****************// class A{ public: A(){ m_num=1; } int m_num; }; void foo (void){ A a; const A &r = a; A a1 = const_cast(a); a1.m_num = 200; cout << a1.m_num << endl;//200 cout << a.m_num << endl;//1 } //****************//
const_cast
expression中的const属性或volatil属性,还能增加const属性或者volatil属性
const int i = 10;
int i1 = const_cast
增加const属性与volatil属性相反.
当前标题:const_cast<type-id>(expression)
网页网址:http://pwwzsj.com/article/gsppph.html