+ 1

Strings and Arrays in Headers

Can a string go in a header file? What about string arrays?

16th May 2018, 1:02 PM
Michelle
Michelle - avatar
2 Answers
+ 5
Sure, why not. You can write your own headers which contain classes, functions, variables, which you can then include in your project / another cpp file. // header.h #include <string> std::string str = "In header file."; // test.cpp #include <iostream> #include "header.h" int main() { std::cout << str; }
16th May 2018, 1:13 PM
Hatsy Rei
Hatsy Rei - avatar
+ 3
Just be careful about allocating storage to static data members of your class. You must do it inside the implementation file (.cpp). Example: // foo.h #include <string> class foo { public: static void f(); private: static std::string _record; // incomplete... }; // foo.cpp std::string foo::_record; // complete allocation void foo::f() { // some code } There are also lots of gotchas regarding to your question which need you to learn more about OOP.
16th May 2018, 1:23 PM
Babak
Babak - avatar