0

In my Qt-project I have some classes (.h and .cpp files). In some of them i use different constants and functions. There are some constants, that I use in 2 or 3 different classes. But I think, that's not really good. So, the question is how to avoid this? Should I create one more class, where I can write all these constants and functions, and include this class? Maybe there is another suggestions?

P.S. For example, functions like abs or sign and constants like const int Scale = 50

OneOne
  • 131
  • 9
  • `abs` is already a standard function (use `` or `` then `fabs`). What is the `Scale` and why should it be a constant (not a tunable runtime parameter)? – Basile Starynkevitch Aug 28 '14 at 08:48

1 Answers1

2

You don't explain what are these constants.

I would make them some #define-d macro, e.g.

 #define MY_AVOGADRO 6.22e+23

or

 #define MY_FIRST_NAME "Basile"

YMMV. I agree that preprocessor macros are low-tech, but they are useful!

So have such #define inside some common header. Take the habit of using some common prefix. In that same header file, declare your global functions or define your static inline functions.

Alternatively, code something like

 const double my_avogadro = 6.22e+23;

You probably don't need to define a C++ class for this.

See also this question.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Macros remain the only solution for a few important tasks, such as #include, #ifdef and #if defined for conditional compilation. You should avoid macros, see C++ Coding Standards: 101 Rules, Guidelines, and Best Practices By Herb Sutter, Andrei Alexandrescu – Marcin Marek Aug 29 '14 at 06:26