const_cast<type-id> (expression)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.