C++: Global classes: prefer static data or not?

Hi,
very sorry if this question should be too picky ... .

I have quite a bit of large "global classes" in my multi-module C++ software. (I mean a class without constructor/destructor, which is used exactly one time in the software - it is defined "globally").

As it is used only one time, I could specify the variables / functions as static or not. I was wondering, which way would create tighter code. According to my investigations I have the following rules now:
- Functions should be static, except inline functions - these must NOT be static (otherwise they are not inlined any more).
- Public data variables, which are heavily used outside class functions, should preferably be defined static.
- Private data variables, which are mainly used inside class functions, should preferably be defined non-static.

Could some C++ expert have a look at these rules and comment them? (ok, quite ok or complete nonsense?)

PS: In some C++ languages, it seems to be possible to define a complete class as static:

static class TestClass{
...
};

As I see, this is NOT possible in Keil C++, or am I wrong?

Parents
  • I was wondering, which way would create tighter code.

    Use straight C.

    - Functions should be static, except inline functions - these must NOT be static (otherwise they are not inlined any more).
    - Public data variables, which are heavily used outside class functions, should preferably be defined static.
    - Private data variables, which are mainly used inside class functions, should preferably be defined non-static.

    Declaring your code/data as static doesn't make it any shorter, so none of these will make your code any "tighter". Besides, if you declare all your data members as static, you will also have to declare as static all your methods that access them. Given that static methods cannot be virtual, why would you need classes at all? So I can only repeat: use straight C and don't cudgel you brains :)

Reply
  • I was wondering, which way would create tighter code.

    Use straight C.

    - Functions should be static, except inline functions - these must NOT be static (otherwise they are not inlined any more).
    - Public data variables, which are heavily used outside class functions, should preferably be defined static.
    - Private data variables, which are mainly used inside class functions, should preferably be defined non-static.

    Declaring your code/data as static doesn't make it any shorter, so none of these will make your code any "tighter". Besides, if you declare all your data members as static, you will also have to declare as static all your methods that access them. Given that static methods cannot be virtual, why would you need classes at all? So I can only repeat: use straight C and don't cudgel you brains :)

Children
More questions in this forum