Notes
C Plus Plus
C Plus Plus
  • Roadmap
    • ๐Ÿ›ฃ๏ธC Plus Plus: Docs
  • Build
    • ๐Ÿ”ฅBuild Process
    • ๐Ÿ”งConnect multiple C++ Files
  • Code
    • โฎ๏ธPre-Processors
      • #include
      • #define
      • #ifdef
      • #pragma
      • Predefined Macros
    • LValue and RValue
    • ๐Ÿช…Data Types
      • Enum
      • TypeDef
      • const in c++
      • extern vs inline
    • ๐ŸŽญCasting
    • ๐Ÿ”ƒOperator overloading
      • Available Operators
      • Examples of operator overloading
    • ๐Ÿ—บ๏ธNamespace
      • Namespace Example
      • Using directive
    • ๐Ÿ•ต๏ธHeader File
      • For C++ Classes
    • ๐Ÿ—๏ธStructure
      • Struct vs Class
      • Public vs Private inheritance
    • ๐ŸขClasses
      • Friend Function
      • Copy Constructor
      • Explicit Constructor
      • Move Constructor
        • Move Semantics
      • Other constructors
      • Virtual functions
      • Pure virtual function
      • Other function declaration
      • const function vs final function
  • Memory
    • ๐Ÿง Memory Introduction
    • โœจHeap and Stack
    • ๐ŸŽฏPointers
      • Dangling Pointer
      • 'this' Pointer
      • Function Pointer
      • Smart Pointers
        • Unique Pointer
        • Shared Pointer
        • Weak Pointer
      • Reference count
    • ๐Ÿ‘จโ€๐ŸญHelper function
    • ๐ŸกVector [ArrayList]
      • Custom vector, part 1
      • Custom vector, part 2
      • Custom vector, part 3
      • std::vector
    • โ™ป๏ธUnion
      • Type Punning
      • Type Punning, part 2
      • Type Punning, part 3
      • Union, part 1
      • Union, Part 2
  • Thread
    • ๐ŸงตThreading
      • std::thread
      • Detach a thread
  • Misc
    • ๐Ÿ—‚๏ธExecution Order
    • ๐Ÿง Print memory
Powered by GitBook
On this page
  1. Code
  2. Data Types

const in c++

Similar to final in Java, we can declare a const variable in cpp, which will than be immutable.

What if i use pointer to change directly on memory?

In C++, when you declare a variable as const, you're promising not to modify its value through that particular name. However, there's a way to circumvent this protection using pointers, though it's considered bad practice and can lead to undefined behavior.

Example:

  1. Direct modification: This is not allowed.

const int x = 5;
x = 10;  // Compilation error
  1. Modification through a non-const pointer: This is possible, but it's undefined behavior.

const int x = 5;
int* ptr = const_cast<int*>(&x);
*ptr = 10;  // Compiles, but is undefined behavior

In this case, the code will compile, but it's not guaranteed to work as expected. The behavior is undefined because:

  • The compiler might optimize away the modification, assuming x is truly const.

  • x might be stored in read-only memory.

  • It violates the const contract, potentially leading to unexpected results.

  1. Modification of const data through a pointer to non-const:

    int y = 5;
    const int* ptr = &y;
    // *ptr = 10;  // Compilation error
    y = 10;  // This is allowed

Here, ptr is a pointer to const int, meaning you can't modify the value through ptr, but you can still modify y directly.

It's important to note that while it's technically possible to modify a const variable using pointers and const_cast, doing so is considered very bad practice and should be avoided. It defeats the purpose of const-correctness and can lead to difficult-to-debug issues.

PreviousTypeDefNextextern vs inline

Last updated 9 months ago

๐Ÿช