title: For unit tests, is there a hackish way to change the value of a const variable?
body: <p>I have a C++ 11 header which has a const value declared as <code>my_const_value</code>. And a function called <code>GetValue</code> that runs a complex logic using the const value and returns an expected value.</p>
  <p>I want to unit test <code>GetValue</code> with different values of <code>my_const_value</code>.</p>
  <p>I know this is not advisable but for writing a unit test for <code>GetValue</code>, I wish to test <code>GetValue</code> with different values of <code>my_const_value</code>. Is there some hack-ish way in C++ to change the value of a const even if it a const?</p>
  <pre><code>//MyHeader.hpp
  namespace myheader {
  
  const int my_const_value = 5;
  
  int GetValue() {
      // In real-world, lets say below line of code is a complex logic that needs to be tested by a unit test
      return /my_const_value * 5) / 25;
  }
  
  }
  
  #include &quot;MyHeader.hpp&quot;
  #include &lt;gtest/gtest.h&gt;
  
  TEST(MyHeaderTest, Testing_Something) {
      EXPECT_EQ(1, myheader::GetValue()); // This is okay
  
      // I want to test that in the future is the value of my_const_value changes to something else then 
      // myheader::GetValue returns the expected result. But of course, I cannot change my_const_value because it is a const.
      // Is there a way to hack around this for a unit test? Is there a way that I could still hack and change the value of my_const_value?
      myheader::my_const_value = 25;
      EXPECT_EQ(5, myheader::GetValue());
  }
  </code></pre>
  <p>I know that I could <code>const_cast</code> <code>my_const_value</code> to a non_const variable. But that wouldn't help here. If there is some hack to change the value of <code>my_const_value</code> by using a pointer or something, that would answer my question.</p>