0

I'm trying to use classes. And as i go also all the global variables and such. That works out. But how do I implement stuff like #define value 100; in the header?

#ifndef COUNT_H_
#define COUNT_H_

// Definitions
#define first 100;
#define second 200;

// Class definition
class Account {
    private:
    int difficult(int);

    public:
    int easy (int);
} ;

#endif /* COUNT_H_ */

Is this a proper way of doing things? It works, but is it also common?

Sam
  • 7,252
  • 16
  • 46
  • 65
  • Yes, it is. What are you bothering about actually? – πάντα ῥεῖ May 18 '14 at 15:54
  • 7
    Don't put semicolons at the end of your defines. Better, don't use `#define`, use proper, typed constants. – Mat May 18 '14 at 15:55
  • if you want to use `macros` in expressions, consider using braces `()` around them when defining. If not mandatory, avoid macros and use `const` variables and inline methods. [learn more](http://www.brainbell.com/tutors/c/Advice_and_Warnings_for_C/Macros_and_Miscellaneous_Pitfalls.html) – Rakib May 18 '14 at 16:00
  • Also, it might be a good idea to not use #ifndef COUNT_H_, etc. on the header. You could use `#pragma once`, or both for maximum portability. Have a look at this: http://stackoverflow.com/questions/787533/is-pragma-once-a-safe-include-guard – djikay May 18 '14 at 16:02

2 Answers2

1

It's fine to declare a header file like that. You can also use #pragma once in Visual Studios, but I personally prefer not to.

Also as Mat said, it's better to use constants over macros for types. Since you don't have to worry about accidental redefining them.

Callum
  • 25
  • 1
  • 4
  • I can see benefits using constants, but then I'll have to rewrite my whole program. That's quite a lot of work since it exits out of 18 c files and 18 headers... Also all is written in functions and not in classes. I'm figuring out what benefit it would have for this specific program. – user2371490 May 18 '14 at 18:15
0

You may use defines such as typed global constants visible anywhere from your code and\or from files that #include this file. Defines are usually used as macros:

#define max(a, b)((a > b) ? a : b)

When the define is used, the preprocessor puts the definition in the place your macros was used:

int i = max(a, b); // translated as int i = (a > b) ? a : b;

Just remember that #define is the preprocessor directive, it is almost the same (of course, except the type definition) as compile-time constants such as:

const int CI = 5;

But it significantly differs from runtime constants:

int foo() { return 451; }
const int CJ = foo()

The reason why you shouldn't put the semicolon in your macros (if it isn't necessary, of course, is that when you do this:

#define first 100;

You've made an assignment "first" is "100;" So this code:

int a = first // <- without semicolon

Will be valid

Netherwire
  • 2,669
  • 3
  • 31
  • 54
  • typing ; was a mistake on my behalve. Came with the haste of typing the OP. Just figuring out now whether to change all my #define's into const for my program (which is about 18 .c and .h files big...) – user2371490 May 18 '14 at 18:17
  • If your program is working the correct way, you don't have to do it. Just keep it in mind later. – Netherwire May 18 '14 at 19:22