Thread: recursion within a class

  1. #1
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179

    recursion within a class

    Is there something about having a class member function try to call itself that will automatically fail that would prevent recursion within a class and how do you avoid it?

    I'm trying to write a c++ conversion of a C program so I can encapsulate a certain functionality within a class, but the functionality relies on recursion to work. But when it comes to the point of making the call it's like it doesn't even run the function and automatically returns a 0. Is this normal? How do I fix it.

    I could show you my code if you like but it's awfully convoluted right now.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, recursion works with member functions just as it does with free functions. Of course, it is always possible to substitute recursion with iteration, using an explicit stack if necessary.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    Okay, this was a classic example of me programming too late at night and shooting of the question that my fatigue prevented me from seeing.

    I had a line in my recursive function that measured how much time it took to run so that it didn't keep dead ends that I haven't programmed it to be smart enough to find the first time. Of course with debugging so I can step through the program the 1 second time limit was expiring before it iterated once.

    So if I could I'd delete this thread. Once again, laserlight, thanks for the quick and knowledgeable reply.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. Replies: 7
    Last Post: 05-26-2005, 10:48 AM
  4. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM