title: Dependent variables in C++?
body: <p>I tried asking before but I wasn't very clear so I'm re-asking it.</p>
  
  <p>I want to have a variable that depends on the value of another variable, like b in this example:</p>
  
  <pre><code>int main(){
      int a;
      dependent int b=a+1; //I'm just making this up
      a=3;
      cout &lt;&lt; b; //prints 4
      a=4;
      cout &lt;&lt; b; //prints 5
  }
  </code></pre>
  
  <p>Of course, this does not exist in C++, but this is what I want.</p>
  
  <p>So instead I tried making a function:</p>
  
  <pre><code>int main(){
      int a;
      int b(){ return a+1; } //error
      a=3;
      cout &lt;&lt; b(); //would print 4 if C++ allowed nested functions
      a=4;
      cout &lt;&lt; b(); //would print 5 if C++ allowed nested functions
  }
  </code></pre>
  
  <p>The above doesn't work because C++ doesn't allow nested functions.</p>
  
  <p>I can only make functions outside of main(), like this:</p>
  
  <pre><code>int b(){
      return a+1; //doesn't work because a is not in scope
  }
  
  int main(){
      int a;
      a=3;
      cout &lt;&lt; b();
      a=4;
      cout &lt;&lt; b();
  }
  </code></pre>
  
  <p>But this does not work because a is not in the same scope as b(), so I would have to pass a as a parameter and I don't want to do that.</p>
  
  <p>Are there any tricks to get something similar to a dependent variable working in C++?</p>