Code of interest

Posted 7 months ago2024-05-02 14:58:25 UTC
Meerjel01 Meerjel01I want to be a Meerjel
#include <iostream>

using namespace std;

class CObject
{
public:
    void ExecuteDeb(int num)
    {
        printf("Printing! %i ", num);
    }
};

class CObjectLinker
{
public:
    CObject* objs;
    int numObjs;

    CObjectLinker(int num)
    {
        numObjs = num;
        objs = new CObject[numObjs];
    }

    void StartExecute()
    {
        int number = numObjs;

        keepExecuting(number);
    }

    void keepExecuting(int& curNum)
    {
        objs[curNum].ExecuteDeb(curNum);
        curNum--;

        if(curNum < 0)
            return;
        keepExecuting(curNum);
    }
};

int main(int argc, char *argv[]) {
    CObjectLinker linker(4);

    linker.StartExecute();
}
Might not be of interest but something.

4 Comments

Commented 7 months ago2024-05-03 13:33:35 UTC Comment #106170
But why recursive?
Commented 7 months ago2024-05-03 15:04:00 UTC Comment #106171
Instead of a For loop I used a countdown method to execute code from an array by a decreasing integer. I think it'll make the program work faster.
Commented 7 months ago2024-05-03 16:45:33 UTC Comment #106172
All that's gonna do is crash once you have a big enough array. All those function calls are gonna add up and eat the stack.
Best to use a for loop in this scenario.
Commented 7 months ago2024-05-03 16:46:46 UTC Comment #106173
Oh. Didn't know about that :|

You must log in to post a comment. You can login or register a new account.