C ++ does not allow int ** to be converted to const int **

Source: Internet
Author: User
C ++ complains about converting 'int *** 'to 'const int **'

Recently when I was switching a project from C to C ++, I found that C ++ compilers do not allow converting int **
To const int **
, Even with explicit cast.

After searching on the web and thinking, I think the underlying reason is that C ++ does not allow converting const T *
To T *
, Unless const_cast
Is used. Let's assume that
Is of Type const
T **
, And B
Is of type T **. If we can assign the value of B
To
, Then we can make *
B
Point to a const t
Variable by dereferencing
, Without using const_cast
. This violates the principles of C ++.

One of the principles of C ++ is that, if a programmer uses const_cast
, He/she declares that the current conversion will do no harm to the programme; otherwise he/she may have not concerned about the possible danger brought by the current conversion, which is probably a bug. in the later case, the compiler must report an error to remind the programmer.

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.