Access static variable from CPP file to other header file

后端 未结 3 1009
青春惊慌失措
青春惊慌失措 2020-12-22 09:02

I\'m trying to access the static variable in other header file from a CPP file.

\"Boo.hpp\"

#ifndef Boo_hpp
#define B         


        
3条回答
  •  我在风中等你
    2020-12-22 09:50

    The keyword static on globals in C/C++ instructs the compiler to limit that symbol to the current compilation unit, i.e. the file it's declared in. Putting static declarations into header files is a huge trap because even though you include the same header in all your source files, each file will receive its own copy of the static symbol.

    What you want is the extern keyword, that declares the symbol to be shared between compilation units, i.e. extern int num.

    Some more reading on storage class specifiers here

提交回复
热议问题