As it is posted, the code should work (this doesn't mean it's good code though). Your issue is probably a circular include (most likely), or a subsequent #undef
.
What is the best way to let class B know the constant from class A?
Use a const int
instead, or an enum
value - defines are so old-school.
Also, does the preprocessor directives only apply to the file it is written in?
It depends - if defined in a file, it applies to that translation unit, from its point of declaration onwards - meaning you can define it in a header, and it will be visible in files that include that header. You can also define preprocessor directives using compiler options, which makes them visible for all files compiled.