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

5
  • How the static functions can be dependent on your object state? Commented Nov 16, 2010 at 7: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. Commented Nov 16, 2010 at 7: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. Commented Nov 16, 2010 at 8: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. Commented Nov 16, 2010 at 9: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. Commented Nov 16, 2010 at 9:18

5 Answers 5

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.

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

Comments

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();
}

2 Comments

Not nice by any means but fulfills the question requirements... I would ommit the default value if setting it is a requirement.
How can you free "a" afterwards, using this solution, supposing it is a (class-)object and not a primitive data type?
1

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

Comments

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.

3 Comments

No, no, don't ensure uniqueness and don't consider the Singleton pattern.
there's usually little point in trying to enforce uniqueness, but many issues arise in the attempt... it's not worth it.
@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.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.