+ 1
Strings and Arrays in Headers
Can a string go in a header file? What about string arrays?
2 odpowiedzi
+ 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;
}
+ 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.