Static class design patterns

Hi, I have a program with a centralized hub - a static class that maintains all high-level program data that isn't gui-related. It also analyzes all user input and file loading/saving.

Originally I wrote a class with all static methods and variables, but as I understand it, it's simpler to just dump everything into a namespace.When I do this I get linking errors ('multiple definitions') on both the functions and variables. What is the preferred design pattern here?
1
2
3
4
5
6
7
8
9
10
11
12
// staticdata.hpp

#ifndef STATICDATA_HPP
#define STATICDATA_HPP

class staticdata
  {
  static int datum;
  static char* salutation;
  };

#endif 
1
2
3
4
5
6
// staticdata.cpp

#include "staticdata.hpp"

int staticdata::datum;
char* staticdata::salutation = "Hello";

Hope this helps.

[edit]
It is also a good idea to put your stuff in a namespace, but I left it out of the example for ease.
Last edited on
Yeah that helps a lot, now what if I wanted to do this without a class, i.e. just dumping everything into a namespace?
Topic archived. No new replies allowed.