title: Class variable resets after function completes
body: <p>I'm new to C++ from a java/python/etc background, and am trying to teach myself OO programming before I have to take a class on it next semester.</p>
  
  <p>I'm trying to make an animation system using SFML, but am having some trouble with one of my class variables; it keeps resetting to 0 after I increment it. I'll start with the code and follow with a log output I'm using to help figure out what's going on.</p>
  
  <p><strong>SOLUTION:</strong> Being new to C++, I was an idiot and returned a new instance of my class in my getter functions; using [class]&amp; func()... instead of [class] func() solved this, but now I have some refactoring to do.</p>
  
  <p>Code (header):</p>
  
  <pre><code>...
  
  typedef std::vector&lt;Frame&gt; frameVect; // (Frame defined above)
  typedef std::vector&lt;double&gt; dubVect;
  
  ...
  
  class limbAnim
  {
  private:
      int limbNum;
      int numFrames;
      int curFrame;
      frameVect frames;
  
  public:
      limbAnim(int limb, int nFrames, frameVect F);
      &lt;getters/setters&gt;
      void incCurFrame();
  
      dubVect incrementAnimation(dubVect curPos, double curRot);
  }
  </code></pre>
  
  <p>Code (cpp):</p>
  
  <pre><code>... (include vector, ofstream, etc)
  
  std::ofstream AnimLog("log.log")
  
  typedef std::vector&lt;Frame&gt; frameVect; // (Frame defined above)
  typedef std::vector&lt;double&gt; dubVect;
  
  ...
  
  limbAnim::limbAnim(int limb, int nFrames, frameVect F)
  {
      limbNum = limb;
      curFrame = 0;
      numFrames = nFrames;
      frames = F;
  }
  
  void limbAnim::incCurFrame()
  {
      curFrame=curFrame+1;
      if (curFrame &gt;= numFrames)
      {
          curFrame = 0;
          AnimLog &lt;&lt; "Greater than." &lt;&lt; std::endl;
      }
  }
  
  dubVect limbAnim::incrementAnimation(dubVect curPos, double curRot)
  {
      AnimLog &lt;&lt; limbNum &lt;&lt; ", " &lt;&lt; numFrames &lt;&lt; std::endl;
  
      if (numFrames &gt; 0)
      {
          AnimLog &lt;&lt; curFrame &lt;&lt; std::endl;
          dubVect curStepP = frames[curFrame].getStepPos();
          double curStepR = frames[curFrame].getStepRot();
  
          curPos[0] = curPos[0] + curStepP[0];
          curPos[1] = curPos[1] + curStepP[1];
  
          curRot = curRot + curStepR;
  
          incCurFrame();
          AnimLog &lt;&lt; "Incremented: " &lt;&lt; curFrame &lt;&lt; std::endl;
      }
  
      dubVect retV = curPos;
      retV.push_back(curRot);
      return retV;
  }
  </code></pre>
  
  <p>So, my log output looks good since I'm testing with 2 frames on limbs 6 &amp; 8, except those limbs' curFrame seems to reset to 0 after incrementing:</p>
  
  <pre><code>...
  5, 0
  6, 2
  0
  Incremented: 1
  7, 0
  8, 2
  0
  Incremented: 1
  9, 0
  ...
  5, 0
  6, 2
  0
  Incremented: 1
  7, 0
  8, 2
  0
  Incremented: 1
  9, 0
  ...(ad nauseam)
  </code></pre>
  
  <p><strong>Edit: Code calling the increment function.</strong>
  (main.cpp)</p>
  
  <pre><code>// (Outside main loop.)
  Animation walk_anim(12, "assets/anim/walk.dat");
  
  // (Inside main loop.)
  for (int i=0; i&lt;12; i++)
  {
      dubVect animDat = walk_anim.getLimbFrame(i).incrementAnimation(limbPos[i], curDegs[i]);
      dubVect newPos = getDVect(animDat[0], animDat[1]);
      double newRot = animDat[2];
      curDegs[i] = newRot;
      if (curDegs[i] &gt;= 360)
          curDegs[i] -=360;
      limbPos[i] = newPos;
  }
  </code></pre>
  
  <p><strong>getLimbFrame</strong></p>
  
  <pre><code>Animation::Animation(int lNum, string fName)
  {
      numLimbs = lNum;
      fileName = fName;
  
      // Fill up limbVect with correct # of empty frames.
      for (int i=0; i&lt;numLimbs; i++)
      {
          frameVect emptyFVect;
          limbAnim LA(i, 0, emptyFVect);
          limbFrames.push_back(LA);
      }
  
      // Boring .dat parsing, populates the 'frames' var of each limbAnim.
      loadAnim();
  }
  
  limbAnim Animation::getLimbFrame(int index)
  {
      if (index &lt; numLimbs)
      {
          return limbFrames[index];
      }
  }
  </code></pre>