0

When I have test data objects, or other static data objects such as SQL strings, I like to keep them out of application code. In Python, this is done with a separate source file and an import as shown below. How is static data organized and used C++?

 (file: testdata.py)
x = Foo() (an object)
x.name = "Bar"
x.number = 123
..

(file: test.py)
import testdata.py
testObject1 = testdata.x 
..
1
  • 1
    What exactly is your definition of "out of the application code"? If you're writing a Python module that contains test data and import it into the application, how is that not part of the application code? Mar 18, 2012 at 18:38

4 Answers 4

2

There are lots of possibilities. This also depends on what you're trying to do (e.g. do you want the values to be editable after compiling or only before compiling?

As for the later, you could use a separate translation unit.

In some header file:

extern const char *name;
extern const int number;

In one separate source file:

const char *name = "Bar";
const int number = 123;

However, you could do the class/struct approach, too:

Header file:

struct Foo {
    const char *name;
    int number;
};

extern const Foo Bar;

Source file:

const Foo Bar = {"Bar", 123};
1
  • Thanks, this works well especially inside a namespace. It seems a lot simpler than static class members.
    – Pete
    Mar 18, 2012 at 20:45
2

The most flexible choice for connection strings etc. would be to create a settings file (with a format of your own choosing) and a class that can read the file, since this allows you to change the settings without recompiling the program.

For static data that you want to include in the code, you can use static class members:

// file.h
class Foo {
public:
    static string getBar();
private:
    static string bar;
};

// file.cpp
Foo::bar = "qwe";

static Foo::getBar() {
    return bar;
}
1
  • 1
    @Griwes: +1; looks like Java and C# have taken their toll on my C++ idioms... :-) Mar 18, 2012 at 22:17
1

You can put them in another file as well; extern data declarations in an header file, and the definitions in a .cpp file, like this:

// Foo.h

extern Foo afoo;
extern int anint;

// Foo.cpp

Foo afoo;
int anint = 0;

Then you can #include "Foo.h" wherever you want to use afoo and anint.

1

There are many ways to store data out of the source code.

For example, configuration files are stored in XML for example, though (fortunately) JSON has been gaining some grounds (not as much as I'd like to), and there are also custom formats.

Another example, for translation you might use gettext and store strings in .po files, and add new languages without recompiling.

It all works the same way, in the end:

  • you define a format for the file
  • you create a reader (and perhaps writer) procedure that converts between the file format and the in-memory format
  • you find a way to pass the file path to the program: fixed name in current directory, fixed name in home directory, command line argument, environment variable, etc...

If you wish to do it as you did in Python, you could also compile the resources into their own DLL and pull them at runtime while loading the symbols, changing the resources would only require re-compiling this one DLL.

2
  • Can you put in a quick example of the DLL technique?
    – Pete
    Mar 18, 2012 at 20:13
  • 1
    @Pete: it simply consists of creating a header with extern declarations of all the entities you might want (or, for a stable ABI, a simple get function taking a string identifier and perhaps some customization options like the language), and then a source file (or several) containing the definitions and linked together in a single dynamic library against which you will link. Mar 19, 2012 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.