1

I have a class which has a number of static function to perform some calculation. However, before the calculation, I need to pass in a data to initialize some of the static data members. Currently I have an init(data) function and a clearResource() function which should be called before and after the use of the class. Is there a better way of doing that?

For example:

classA(){
 static int a;
 static init(int b) {
    a = b;
 }
 static functionA(){
   //perform something based on value of a;
    switch(a){
    }
 }

}

int main(){
  classA::init(5);
  classA::functionA();
 }

Thanks

Leslieg
  • 11
  • 3
  • How the static functions can be dependent on your object state? – yadab Nov 16 '10 at 07:35
  • It's not clear whether you want to use `init` and `clearResource` for each object of your class or only once for all objects. – Alexey Malistov Nov 16 '10 at 07:40
  • Well if you have a function that is dependent on state then you should `wrap the state and the function` into its own class that encapsulates all this information. Then the constructor/destructor of this new class will handle all the above automatically. – Martin York Nov 16 '10 at 08:15
  • You should really revisit your design. Note that just the description is a clear indication that your program will never support multithreading (a static variable has to be modified in each thread that uses `functionA`, both before entering and after completion. If a second thread tries to call the function while the first thread is inside, it will change the *global* while in the middle of the operation. – David Rodríguez - dribeas Nov 16 '10 at 09:00
  • Thanks for the reply @David: Could you elaborate a bit on the multithreading part? I do not quite understand it. I didn't need to modify the value of the int a and it only needs to be init once at the beginning of the programme. Would that still give problems? Thanks. – Leslieg Nov 16 '10 at 09:18

5 Answers5

2

Avoid using static member functions : have your constructor initialize the data and the destructor clear the resources (see RAII). If the existing class cannot be changed, implement a helper class which calls init from its constructor and clearResource from its destructor.

icecrime
  • 74,451
  • 13
  • 99
  • 111
2

You can use this kind of design

class A()
{
public:
 static int a;
 static void functionA(int arg = A::a)
 {
  if(A::a != arg)
   A::a = arg;
  ...
 }
};

int A::a = 0;
int main()
{
 A::functionA();
}
Mihran Hovsepyan
  • 10,810
  • 14
  • 61
  • 111
1

You should apply the RAII concept: see this and this questions.

Community
  • 1
  • 1
Simone
  • 11,655
  • 1
  • 30
  • 43
1

Make the member functions and data non-static, initialize in a constructor and free resources in the destructor. This will guarantee the correct sequence of calls: initialize - perform operations - free resources in the client code.

vitaut
  • 49,672
  • 25
  • 199
  • 336
  • 2
    No, no, don't ensure uniqueness and don't consider the Singleton pattern. – Puppy Nov 16 '10 at 07:52
  • there's usually little point in trying to enforce uniqueness, but many issues arise in the attempt... it's not worth it. – Matthieu M. Nov 16 '10 at 07:58
  • @DeadMG: It is unlikely that the OP needs uniqueness in this particular case so I removed the reference to Singleton. Although I wouldn't be so sure without seeing the actual (not example) code. – vitaut Nov 16 '10 at 08:02
1

I'd avoid using static members in this case.

This is your problem. You have a class that does processing on some data. That data, for whatever reason, needs to be shared across all instances of this processing class. Ok then, we have a non-static solution!

class Data : boost::noncopyable
{
public:
    Data()
    {
        // initialise all of our data.
    }; // eo ctor
}; // eo class Data

Where you instantiate this class is up to you. It could be a member of an application class that is run at start up, or part of some root. It just needs to be accessible and does not need to be static nor a singleton.

class DataProcessor
{
private:
    Data& m_Data;

public:
    DataProcessor(Data& _data) : m_Data(_data)
    {
    }; // eo ctor
}; // eo class DataProcessor
Moo-Juice
  • 38,257
  • 10
  • 78
  • 128