Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualKing Mir

Posted 11 February 2013 - 03:30 PM

'array' is a keyword in C++ IIRC, so it's a good habit to not use it as a variable name. Rather than using #define to define constants it's better to just make constants:

const size_t SIZE = 10;
This gives you type control and avoids macro expansion problems like the one you encountered.

Using #define is almost the same as using your a text editor's 'find-and-replace' function. There are some cases where it's useful, but usually it causes more problems than it solves.

The problem with const size_t is that a global variable defined like that is addressable, and must be generated in the object file. #define, for it's faults, does not have this problem. Therefore I consider a #defined macro to be the preferred general purpose way to declare constants in C.

 

C++ has slightly different rules for global constants, so there the convention is different; in c++ global constants have internal linkage, and do not allocate storage if nothing takes their address.


#1King Mir

Posted 11 February 2013 - 03:21 PM

'array' is a keyword in C++ IIRC, so it's a good habit to not use it as a variable name. Rather than using #define to define constants it's better to just make constants:

const size_t SIZE = 10;
This gives you type control and avoids macro expansion problems like the one you encountered.

Using #define is almost the same as using your a text editor's 'find-and-replace' function. There are some cases where it's useful, but usually it causes more problems than it solves.

The problem with const size_t is that a global variable defined like that is addressable, and must be generated in the object file. #define, for it's faults, does not have this problem. A more typesafe solution would be to stick SIZE in an enum, but I consider a #defined macro to be the preferred general purpose way to declare constants in C. C++ has slightly different rules for global constants, so there the convention is different.


PARTNERS