InterviewSolution
| 1. |
What Is Meant By Const_cast? |
|
Answer» The const_cast is used to convert a const to a non-const. This is shown in the following program: #include VOID main( ) { const int a = 0 ; int *ptr = ( int * ) &a ; //one way ptr = const_cast_ ( &a ) ; //better way }Here, the address of the const variable a is assigned to the POINTER to a non-const variable. The const_cast is also used when we want to change the data members of a class inside the const member functions. The following code snippet SHOWS this: class sample { private: int data; public: void FUNC( ) const { (const_cast (this))->data = 70 ; } };The const_cast is used to convert a const to a non-const. This is shown in the following program: Here, the address of the const variable a is assigned to the pointer to a non-const variable. The const_cast is also used when we want to change the data members of a class inside the const member functions. The following code snippet shows this: |
|