title: How can I create a switch for class members?
body: <p>Say I have a class with a couple of data members, and I want a class method that returns one, and the next time it is called returns the value of the other. Something like:</p>
  
  <pre><code>class MyClass
  {
      public:
          MyClass():switch(0){};
          int get();
      private:
          int intA, intB;
          int sw;
  };        
  int MyClass::get()
  { 
      if ( (++sw)%2 )
          return intA;
      else
          return intB;
  }
  </code></pre>
  
  <p>What would a more elegant way of doing it be? I don't like the if...else statement very much. It's fine for something like return, but if I'm actually using more complex operations, I end up duplicating a ton of code. Or having to create a second method within each method that is called after I resolve what element I'm pointing to.</p>
  
  <p>What I'd prefer to do, ideally, is to use some form of pointer, so I can do</p>
  
  <pre><code>class MyClass
  {
      public:
          MyClass():switch(&amp;intA){};
          int get();
          void toggleSwitch();
      private:
          int intA, intB;
          int * sw;
  };        
  int MyClass::get()
  {
      return *sw;
  }
  void MyClass::toggleSwitch()
  {
      if ( sw == &amp;intA )
          sw = &amp;intB;
      else
          sw = &amp;intA;
  }
  </code></pre>
  
  <p>Or something to that effect. I could call toggleSwitch(), and have my class operate on either one or the other value easily.</p>
  
  <p>I still don't like it though. I prefer to avoid if's when possible, and I shouldn't need one in this case. This use of a naked pointer should be pretty safe, but I was thinking I could have something like std::unique_ptr holding each element and then std::swap them. But then the pointers would own the elements, and they'd be dynamic memory instead.</p>
  
  <p>So is there a better way to do it?</p>