-1

I used Macros at top of class to tell what is 1 and 45.00 instead of just writing magic numbers.

Question is why using Macros in following class (see the default constructor) is not a good idea? Why using const int LIMIT_UNDER = 1 better? I think there is no difference.

But if I do use that constant at class data member level(global const is not a good thing we know), i.e.const int LIMIT_UNDER, will I initialise it using constructor initializer list?

Class details:-

The Cube class which I have to calculate volume also. The class can have a user input of FLOAT length, width and height in between minimum 1 to maximum 45.00. Otherwise if user inputs any other numbers its all sides would be 1.

so it will be like

// Use MACRO or CONST???
#define LIMIT_UNDER 1
#define LIMIT_UPPER 45.00

class Cube {
  float height;
  float width;
  float length;

public:
  // Write setter getters here

  // default constructor
  Cube() {
    // default constructor puts default values
    height = width = length = LIMIT_UNDER;

  // Other code of class...
  )
Retired Ninja
  • 4,785
  • 3
  • 25
  • 35
  • 2
    If global const is a bad thing then macros are even worse. At least you could contain the constants in a namespace if you wanted to and they'd have a type. – Retired Ninja Sep 19 '22 at 00:43
  • @RetiredNinja How about declaring and initialize them as class data members and initiatlize them using constructor. It will be much of an effort though :( better to use your opinion of namespace then :) – codingnoobs Sep 19 '22 at 00:50
  • When you trace your code in a debugger, it will not show the values of LIMIT_UNDER and LIMIT_UPPER. – 273K Sep 19 '22 at 00:59
  • 1
    @273K - You can actually get macros in your debugger with gcc/gdb if you crank the debug symbols high enough (`-ggdb3` works for sure, I haven't tested other flags) – Stephen Newell Sep 19 '22 at 01:01
  • @Stephen Please show me how to enable -ggdb3 in msvc. When I said "debugger", I didn't mean gdb. – 273K Sep 19 '22 at 01:15
  • Global constants are not a problem. Global *mutable* variables are the problem. The problem with globals is not the scope. – molbdnilo Sep 19 '22 at 01:21
  • @273K - Note where I said "with gcc/gdb". I agree with your sentiment regarding macros making it harder to debug, but it's a not a universal truth that the values won't show up in debuggers. – Stephen Newell Sep 19 '22 at 01:22

1 Answers1

1

#define constants in header files are generally considered bad because they pollute all files that happen to include that header. Even worse, their value could be changed by other files via #undef.

Instead of using globally accessible symbols, try to keep things local to the scope in which they matter. For example:

class Cube {
  static constexpr float minLimit = 1.f;
  static constexpr float maxLimit = 45.f;

  float height = minLimit;
  float width = minLimit;
  float length = minLimit;
};

This will also implicitly create a constructor that initialises the members to the default value. No need to write a constructor manually.

bitmask
  • 32,434
  • 14
  • 99
  • 159