0

here is my code:

#include <iostream>
using namespace std;

int main()
{
    const int a = 10;
    cout << a << endl;

    int *b = const_cast<int *>(&a);
    cout << *b << endl;
    (*b)++;
    cout << *b << endl;
    cout << a << endl;

    return 0;
}

output on ubuntu20, g++ 9.3.0
-----
10
10
11
10

the output puzzles me. pointer b points to int a. I have cast it to int *. why the output for a is not changed and the b has changed. what's the mechanism of const_cast working. I am a new learner of cpp, I used java.

Melvin Levett
  • 341
  • 2
  • 3
  • 11
  • 1
    *I am a new learner of cpp, I used java* -- C++ has a concept that doesn't exist in Java -- *Undefined behavior*. – PaulMcKenzie Oct 31 '20 at 16:12
  • You are only allowed to cast `const` away if the value initially was not const. For `const int a = 10;` the `a` is `const` right from the beginning, so the compiler is allowed to assume that `a` will never change, and can optimize `cout << a << endl;` to `cout << 10 << endl;` – t.niese Oct 31 '20 at 16:18
  • @PaulMcKenzie In my opinion, my case code should be the raw semantics of const_cast. If const_cast can not achieve this simple and straightforward function, why defined it as its name showing? It's really strange. – Melvin Levett Oct 31 '20 at 16:18
  • @t.niese I have know my error understanding, thanks. If the const_cast raw semantic is as your word. my case output should be reasonable. – Melvin Levett Oct 31 '20 at 16:20
  • You are attempting to change something that is const. Doing things such as casting the constness away and changing the value using an aliased pointer is undefined behavior. The code where you attempted to change the value could have even crashed, depending on the compiler, compiler settings, etc. – PaulMcKenzie Oct 31 '20 at 16:21
  • @PaulMcKenzie I want to say that cpp is really complex and strong, after my few days learning. – Melvin Levett Oct 31 '20 at 16:22
  • 3
    @MelvinLevett if you started to use `const_cast` after a few days of learning, then you most certanly use the wrong source/way of learning. `const_cast` only has use in rare edecases and is normally a topic at later chapers of books or courses. – t.niese Oct 31 '20 at 16:25
  • @MelvinLevett C++ allows you to compile code like this, which is why it is different than Java. It's the same thing with arrays -- C++ allows you to write to array elements out-of-bounds, while Java doesn't. The problem is that writing to an out-of-bounds element in C++ is -- yes -- undefined behavior. There are countless other examples of this. – PaulMcKenzie Oct 31 '20 at 16:27
  • @t.niese I learn cpp by school class lectures. the code is my inspire after I read the section of RTTI. – Melvin Levett Oct 31 '20 at 16:29
  • 2
    @MelvinLevett I don't know how the lecture of you school is. But out of my experience, most lecutres in schools and even universities are done by people who only have a basic knowledge about c++, and are often of bad quality with many wrong suggestions and errors. If you really want to learn c++ you should take a look at [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242) – t.niese Oct 31 '20 at 16:34
  • @t.niese In fact, I have buy the book c++ primer, but it's too heavy. I just go through the cpp by lectures to have a entire framework of cpp, because I already have the knowledge of oop and structure programming experience. I will read the book as listed to research more. – Melvin Levett Oct 31 '20 at 16:44
  • `[…]I already have the knowledge of oop and structure programming[…]` there is not one OOP. There are many deign patterns and I'm pretty sure - becaues you are a beginner - you only have a small insight to them. Which patterns you should use and how depends on the programming language. Things that are best practice in Java can be an anti pattern in C++. If you want to become good at a language you need to first learn its basics and its spirit, instead of trying to apply your knowledge of an other language. – t.niese Oct 31 '20 at 16:58

0 Answers0