5

Lets say I have function like this:

void processElement() {
    doSomething(someArray[lastProcessedElement + 1]);
}

The thing is, every time this function called, I need to the store the last element that I called doSomething on. So in here I have two choices:

  • I can create a private class variable named lastProcessedElement and increment it's value every time that function is called. This approach seems most common. So the code can be something like this:

    class Foo {
        int lastProcessedElement = 0;     
    
        public:  
        void processElement() {
            doSomething(someArray[++lastProcessedElement]);
        }
    }
    
  • As second option, I can create a static variable in function and increment it every time:

    // Class is not important here, so the function is:
    void processElement() {
        static int lastProcessedElement = 0;
        doSomething(someArray[++lastProcessedElement]);
    }
    

The first solution adds a little bit complexity which I don't want. I like to keep things in-place.

I know the second solution only works if that class have only one instance.

So using static variable method is a good solution? And is there any in-line solution to multi-instance class? (There can be a solution to this particular array element index thing, but I just made that up, I'm talking about storing some value for the next call of the function)

2
  • You could also pass the lastProcessedElement into processElement as a reference. Commented Jul 13, 2016 at 13:25
  • Let's say the caller doesn't know which element to process. Commented Jul 13, 2016 at 13:28

1 Answer 1

5

You already figured out why the function-scoped static is a bad idea:

only works if that class have only one instance

That's a bad design limitation.

Just keep the state as a regular class member variable.

Sign up to request clarification or add additional context in comments.

5 Comments

but then it is mandatory to carry this object whenever it needed.
@HumamHelfawi: What do you mean? If it is "needed" then of course it is "mandatory."
So you are saying there isn't any in-line solutions for this. What about using static variable solution in one-instanced(I mean, if I know that class has only one instance for sure) classes. Are there any other pitfalls for this situation?
I mean if you need to call this function from some other function, you have to instance an object of the Foo class. and if you want to use it from another function, you can not just instance a new one you have to still passing it from place to another each time.
@isamert: The pitfall is that there is no such thing as a class which will definitely only have one instance. Design requirements change. Singletons are not good.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.