-1

I want to create classes where each class is associated with some function. I want to stack these functions in series such that one function runs after another in a user defined order. I am new to C++ so correct me wherever my syntax is wrong.

#include<iostream>
using namespace std;
class Function
{
public:
    virtual void setSuccessor(Function *) = 0;
    virtual void Start(int value) = 0;
};

class Prime:public Function
{
public:
    Prime()
    {
        successor=NULL;
    }
    Function *successor;    
    void setSuccessor(Function *f)
    {
//if a successor is not declared then assign successor.
//else if a successor is already defined then set the new function as the successor of the successor.
        if (successor == NULL)
            successor = f;
        else
            successor->setSuccessor(f); 
    }

    void Start(int value)
    {
        //prime function
        cout<<"I am Prime Function\n";
        //if successor !=null
        //  call successor
        if(successor!=NULL)
            successor->Start(value);
    }

};
class Even:public Function
{
public:
    Even()
    {
        successor=NULL;
    }
    Function *successor;    
    void setSuccessor(Function *f)
    {
        if (successor == NULL)
            successor = f;
        else
            successor->setSuccessor(f); 
    }

    void Start(int value)
    {
        //prime function
        cout<<"I am Even Function\n";
        //if successor !=null
        //  call successor
        if(successor!=NULL)
            successor->Start(value);
    }

};

class Pipeline
{
    public:
    Function *function;
    void addToPipe(Function* function)
    {
        if(this->function==NULL)
            this->function=function;
        else
            function->setSuccessor(function);
    }
    void start(int value)
    {
        function->Start(value);
    }
};
int main()
{
    Pipeline pipe;
    pipe.addToPipe(new Prime());
    pipe.addToPipe(new Even());
    pipe.start(5);
}

In this program I have stacked Prime first then Even. I want that my function prime starts then Even starts. Since no other function is added so it should stop after Even. This program has runtime errors. Please help me detect them

4
  • Is there a reason that you try chain those Function objects using setSuccesor instead of adding them to a std::list in your pipeline?
    – t.niese
    Feb 17, 2020 at 16:01
  • 1
    And what is wrong with your code? If you only want it reviewed, there is [another site] for exactly this purpose. If you encouter errors please tell us what's wrong.
    – churill
    Feb 17, 2020 at 16:08
  • yes I want to set them using set successor. I am going to use this idea in a bigger program which cannot use list/vectors/ etc
    – Lengdon
    Feb 17, 2020 at 16:09
  • This code has runtime errors. It exits after sometime without displaying anything.
    – Lengdon
    Feb 17, 2020 at 16:11

2 Answers 2

1

Found two flaws in your program (could be reasoned to be simple typos):

  1. Pipeline::function is never initialized, so it has some garbage value, which causes undefined behaviour when dereferencing it (for example a crash). Simply initialize it to NULL or preferably nullptr like you do in the other two classes.

  2. In Pipeline::addToPipe you forgot a this:

    void addToPipe(Function* function)
    {
        if(this->function==NULL)
            this->function=function;
        else
            function->setSuccessor(function); // Calls setSuccessor on the local variable!!
    } 
    

With those two fixed I received the (expected) output

I am Prime Function
I am Even Function
0
1

You may use standard library templates for this and avoid writing your own classes. For example,

# include <iostream>

void prime(int) { cout << "Prime" << endl; }
void even(int) { cout << "Even" << endl; }

...

#include <functional>
#include <vector>

std::vector<std::function<void(int)>> pipeline;
pipeline.push_back(prime);
pipeline.push_back(even);

...

for (const auto& f : pipeline) f();

Note that it seems odd to have functions named "prime" and "even" that return void.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.