0

1) I have created a class that contains only static methods.
Is such a class useful, or should I replace it with a namespace ?

2) In that class, I have created a const array member variable (also static).
I had to redeclare in the cpp, then I have a function to fill it. (also a bool to check if it is initialized)

void ThatClass::initializeArray()
{
    if (!arrayInitialized)
        initailizeIt();
    else
        qDebug("was initialized");
}

If I run the function more than once, I get the debug message, which tells me that the class is being held in memory (even though I never explicitly called its constructor and I am not holding it in the caller explicitly).

In this case, it seems worth using a class instead of a namespace....
EXCEPT...

Wouldn't it be more effective to have a namespace, and for the function that requires the specific const, to have either a namespace level const or a const inside the function ?

I am concerned by memory usage and speed, so in this case (situation 2) what are the advantages and disadvantages of

a) using a class with static functions and static members
b) using a namespace with a namespace-level const
c) using a namespace with const inside the (one) function using it

I have tried to find info on this - and found very useful learning information - but nothing to compare using classes with only static methods, versus not using a class at all.

Thalia
  • 13,637
  • 22
  • 96
  • 190
  • a) See http://stackoverflow.com/questions/1434937/namespace-functions-versus-static-methods-on-a-class b) A part of your question makes no sense, eg. the function which should change a const variable. c) Performance is not different for static class methods or namespace functions. – deviantfan Oct 27 '15 at 15:32
  • @deviantfan how would static const variables be initialized ? It should be in constructor... except would the constructor be called ? The bool I had made the variable be only given value once... so it behaved like a const... I see now it is confusing, or I am confused :-). Thank you for the link. – Thalia Oct 27 '15 at 15:47
  • A `static` variable in a class is initialized before it's class constructor is relevant. And do you have a variable which is `const` or just a variable which isn't changed after initialization (but could be changed)? That's a big difference. – deviantfan Oct 27 '15 at 15:49
  • @deviantfan - the variable is not marked const. the only way I figured how to initialize it was with a function call. But - comparing (b) and (c) - if I have a namespace how would I initialize such a variable ? and if it is function level, isn't it going to be slower if I create it on each function call ? – Thalia Oct 27 '15 at 15:51
  • A static variable inside a function is *not* created every function call, only the first time. And see http://stackoverflow.com/questions/5019856/c-initialize-static-variables-in-class – deviantfan Oct 27 '15 at 15:54
  • @deviantfan Oh so if I make namespace I can make static variable for my array. For static member variables, none of the examples can work in my case since I must initialize an array based on some calculations. But I suppose calling the initializer function from constructor may be better and then i don't need the bool to check if the array was initialized. Thank you – Thalia Oct 27 '15 at 16:00
  • And I still don't understand the problem. Make a `static const std::vector mySomething`, in a namespace or class or anywhere, a calculation function `initCalc` returning a vector, and initialize `mySomething` with `initCalc()` – deviantfan Oct 27 '15 at 16:03
  • @deviantfan "and initialize `mySomething` with `initCalc()`" - where ? I assume, in the class constructor like I said, right ? – Thalia Oct 27 '15 at 16:05
  • Again, `static const` variables can't be modified in class constructors. And it doesn't matter (for this problem) if it is a class or namespace variable. How static variables are initialized is the topic of the last link, 4 comments above – deviantfan Oct 27 '15 at 16:07

0 Answers0