LoginSignup
3
3

More than 5 years have passed since last update.

[自分用メモ]C++で重複定義を回避する

Posted at

#ifndef#endif#defineを使う。

1. ヘッダファイル(.h)の先頭に次の2行を入れる

#ifndef INCLUDED_Sample_h_
#define INCLUDED_Sample_h_

2. ヘッダファイルの最後に次の1行を入れる

#endif

3. ソースの全体

#ifndef INCLUDED_Sample_h_
#define INCLUDED_Sample_h_

const int i = 100;

#endif

するとどうなるか...

1回目のconst int i = 100はコンパイルされるが、2回目は無視される。
つまり1つのプログラムでヘッダファイルが何回インクルードされても、定義は1回だけとなる。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3