Why use #ifndef CLASS_H and #define CLASS_H in .h file but not in .cpp?

后端 未结 9 1293
谎友^
谎友^ 2020-12-02 03:30

I have always seen people write

class.h

#ifndef CLASS_H
#define CLASS_H

//blah blah blah

#endif

The question is, why don\'t they

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 04:08

    its because of Headerfiles define what the class contains (Members, data-structures) and cpp files implement it.

    And of course, the main reason for this is that you could include one .h File multiple times in other .h files, but this would result in multiple definitions of a class, which is invalid.

提交回复
热议问题