Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 499109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:57:07+00:00 2026-05-13T05:57:07+00:00

I have a member function of an object that is typically used in an

  • 0

I have a member function of an object that is typically used in an iterative manner but occasionally in a recursive manner. The function is basically following the path of a stream of water downhill, and under certain conditions the stream could split. In order to support the occasional recursion I have to push the state of the object onto the stack and pop it off afterwords. I’m currently doing this using local variables, but the code looks awful. I’d like to write PushState and PopState members but most of the implementations I’ve looked at aren’t great. e.g. using a stack member in my object which ends up using the heap which is slow and leads to other issues. _alloca won’t work as the stack frame is lost when the push function returns.

Are there any other reasonable generic ways of pushing and popping from the stack that i’m missing?

class CMyObject
{   
    enum Direction
    {
      left,right,branch,finished;
    }

    // state variables
    double m_XPos,m_YPos;

    void    Navigate(double x,double y);
    Direction GoLeft();
    Direction GoLeft();
};

void    CMyObject::Navigate(double x,double y)
{
    m_XPos = x; m_YPos = y;

    Direction d = GoLeft(x,y);
    while(d != finished)
    {
        switch(d)
        {
            case left: d = GoLeft(); break;
            case right: d = GoRight(); break;
            case branch:
            {
                // push object state onto the stack
                double temp_x = m_XPos; double temp_y = m_YPos;
                Navigate(m_XPos,m_YPos);
                // pop object state from the stack
                m_XPos = temp_x; m_YPos = temp_x;
                GoRight();
            } break;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:57:07+00:00Added an answer on May 13, 2026 at 5:57 am

    I guess the obvious way would be to create a copy of your entire object and the do the recursive call on that. This way, each branch has it’s own state, and you compiler does the stack management for you. Basically, a method has to be reentrant in order to be safely used in recursion. That is not the case here, since your methods rely on a persistant state. So you have to copy that state, and that means copying the object.

    UPDATE:

    Some more links about recursion and reentrancy:

    • this article is about reentrancy in emedded systems, but also explains the realtion to recursion
    • Wikipedia on the subject
    • another Article(pdf) by the author of the first one

    Sadly, Donald Knuth has not yet finished the fourth part of his masterpiece, but when he does, read it, for it certainly clear that up.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a member function in a class that has a callback, but the
Say I have a member function that creates a new object and places it
I currently have a function: public static Attribute GetAttribute(MemberInfo Member, Type AttributeType) { Object[]
I have a template class with a variadic template member function that I am
I have a template member function of a class that I'm using for adding
I have the following definition of a boost::function object: typedef boost::function<std::string (std::string, std::string)> concat;
have a Grails domain object that has a custom static function to grab data
Suppose I have a class called Object. The Object class has a member function
I have an inline member function defined under class MyClass int MyClass::myInlineFunction(); This function
I wonder if it is a good practice to have a member template function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.