4

I have a macro definition in MyClass.h, stated as such:

#define _BufferSize_ 64

I placed the include directive for MyClass.h inside of main.cpp:

#include "MyClass.h"

Does this mean I can use _BufferSize_ in both main.cpp and MyClass.h? Also, is this good practice?

digito_evo
  • 3,216
  • 2
  • 14
  • 42
Tryb Ghost
  • 419
  • 1
  • 4
  • 14
  • 2
    what happened when you tried? Good practice? Definitely no! Dont use macros when you can write the same in code. Macros are best not considered as part of your code, but rather part of some automatic code generation and the code that is generated is something you usually do not look at – 463035818_is_not_an_ai Feb 26 '19 at 11:17
  • `Does this mean I can use BufferSize in both main.cpp and MyClass.h?` **YES** (Actually NO, but \_BufferSize_ YES); `Also, is this good practice?` **Depends but mostly NO** It is not good practise in C++ to use Macros, except you have to. It is not good practise to user _underscore_ in beginning of anything in C++. – user1810087 Feb 26 '19 at 11:19
  • This doesn't address the question, but names that begin with an underscore followed by a capital letter (`_BufferSize_`) and names that contain two consecutive underscores are reserved for use by the implementation. Don't use them in your code. – Pete Becker Feb 26 '19 at 14:43

4 Answers4

4

Yes, it would work. (Disregarding the problem with underscores that others have pointed out.)

Directive #include "MyClass.h" just copies the whole content of file MyClass.h and pastes it in the place of the #include. From the point of view of the compiler there is only one source file composed of the file specified by the user and all included files.


Having said that, it would be much better if you use in-language construction instead of preprocessor directive. For example replace:

#define _BufferSize_ 64

with

constexpr size_t BufferSize = 64;

The only thing it does differently than the #define is that it specifies the type of the value (size_t in this case). Beside that, the second code will behave the same way and it avoids disadvantages of preprocessor.

In general, try to avoid using preprocessor directives. This is an old mechanism that was used when c++ coudn't do that things in-language yet.

Piotr Siupa
  • 3,929
  • 2
  • 29
  • 65
3

Yes, that is the purpose of header files: creating declarations and constants in one file that you can "include" into translation units whenever you like.

However, your macro name is illegal, and a nice constexpr size_t BufferSize = 64 would be more idiomatic nowadays; even before recent versions of C++, a typed constant would be preferable to a macro in many cases.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
2

First, regarding the identifier _BufferSize_, the standard states that:

3. ...some identifiers are reserved for use by C++ implementations and shall not be used otherwise; no diagnostic is required.

(3.1) Each identifier that contains a double underscore __ or begins with an underscore followed by an uppercase letter is reserved to the implementation for any use.

So having such an identifier in your code would lead to undefined behavior.

And as already suggested in the comments, using macro variables is not good practice in C++. You can use a const int instead.

Community
  • 1
  • 1
P.W
  • 26,289
  • 6
  • 39
  • 76
0

Replying 3 years later because the answers are wrong and this is first google search result in certain keywords.

https://google.github.io/styleguide/cppguide.html#Preprocessor_Macros

Avoid defining macros, especially in headers; prefer inline functions, enums, and const variables. Name macros with a project-specific prefix. Do not use macros to define pieces of a C++ API.

Highlight by me, not in original text.

demonoga
  • 43
  • 7