22

Value of constant variable can be changed through pointer tricks, but is it possible to do something like this :

    class A (){
       int x;
    public:
       void func () const {
          //change value of x here
    }
}
  • 1
    *Value of constant variable can be changed through pointer tricks* - Sure, but it's not useful at all. Just because you can trick the compiler into that doesn't mean it's safe. – chris Sep 21 '15 at 01:49
  • i understand, just asked out of curiosity and knowledge of tricks –  Sep 21 '15 at 01:51
  • 1
    Yes and safe if you mark `x` as `mutable int x;` – vsoftco Sep 21 '15 at 01:53
  • 3
    @jacky666 You see, the use of `const` is generally optional, its mostly something you use to tell the compiler a determined method is "read-only" and guaranteed to execute without changing any data in the class. When you violate that premise you defeat the purpose of defining a method as `const` to begin with. Don't do it. If your method does in fact change data in the class, then don't make it `const`. – Havenard Sep 21 '15 at 01:54
  • 1
    The search is your friend http://stackoverflow.com/questions/751681/meaning-of-const-last-in-a-c-method-declaration – StahlRat Sep 21 '15 at 02:08

6 Answers6

66

declare x mutable

class A (){
   mutable int x;
public:
   void func () const {
      //change value of x here
   }
}; 
john zhao
  • 986
  • 1
  • 6
  • 8
13

You have two options:

class C
{
public:
    void const_f() const
    {
        x = 5;                               // A
        auto* p_this = const_cast<C*>(this); // B
        p_this->y = 5;
    }

private:
    mutable int x;                           // A
    int y;
};
  • A: declare certain members mutable.
  • B: const_cast to remove constness from the this pointer.
bku_drytt
  • 3,169
  • 17
  • 19
8

Though this is not appreciated, but C++ provides “Backdoors” which can be used to breach its own regulations, just like dirty pointer tricks. Anyway, you can easily do this by using a casted version of “This” pointer :

class A (){
           int x;
        public:
           void func () const {
              //change value of x here
         A* ptr =  const_cast<A*> (this);
         ptr->x= 10;     //Voila ! Here you go buddy 
        }
 }
Adeel Ahmad
  • 990
  • 8
  • 22
6

The most important thing to understand here is bitwise/physical/concrete constness and conceptual/meaningwise/logical/abstract constness.

In short:

  • If the function is conceptually const, make the member data mutable.
  • Otherwise, make the function non-const.
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
0

Just cast 'this', this would be a dirty way to implement your program, do avoid this if you are doing a project or teamwork as others would get confused by this.

    class CAST_CLASS (){
       int var;
    public:
       void change_CAST () const {

     CAST_CLASS* pointer =  const_cast<CAST_CLASS*> (this);
     pointer->var= 10;     
    }};
Tayyab Kazmi
  • 376
  • 4
  • 19
0

The other answers don't mention this, but following also modifies "x" (definitely, not advisable):

class A {
    int x, &y{x}, *z{&x};
public:
    void func () const
    {
        y  = 42; // x is modified now!
        *z = 29; // x is modified again!!
    }
};
Cheshar
  • 571
  • 1
  • 7
  • 21