0

I lack of experience with C++ and I am trying to create a Settings file to put all my definitions and global variables there, so my project's classes can access those the values from there. The Settings.h file could look like this:

#ifndef SETTINGS_H_
#define SETTINGS_H_

#define COLOR_BLUE = Vec3b(255, 0, 0);
#define COLOR_GREEN = Vec3b(0, 255, 0);
#define NOT_SET = 0;
#define IN_PROCESS = 1;
#define SET = 2;
#define FGD_PX = 255;
#define BGD_PX = 127;

#include <cv.h>
using namespace cv;

class Settings {
};
#endif /* SETTINGS_H_ */

The idea is to access the variables without instantiate the class but just including it.

Is there any beautiful way to do this?

cheers,

marcelosalloum
  • 3,481
  • 4
  • 39
  • 63

3 Answers3

2
#include <cv.h>
using namespace cv;

#ifndef SETTINGS_H_
#define SETTINGS_H_

#define COLOR_BLUE Vec3b(255, 0, 0)
#define COLOR_GREEN Vec3b(0, 255, 0)
#define NOT_SET 0
#define IN_PROCESS 1
#define SET 2
#define FGD_PX 255
#define BGD_PX 127

class Settings {
public:
   static int var1;
   static float var2;
   static short var3;
};

// initialization
int Settings::var1 = SET;
float Settings::var2 = 3.14;
short Settings::var3 = BGD_PX;

#endif /* SETTINGS_H_ */

Usage:

int tmp = Settings::var1;
2

Constants either go in enums, or as static consts. Manifest constants generally are reserved for compiler type options:

#ifndef SETTINGS_H_
#define SETTINGS_H_

class Settings {
public:
   static const vec3b color_blue;
   static const vec3b color_green;
   enum statics {
         NOT_SET = 0,
         IN_PROCESS = 1,
         SET = 2,
         FGD_PX = 255,
         BGD_PX = 127
   };

};

vec3b Settings::color_blue(255, 0, 0);
vec3b Settings::color_green(0, 255, 0);

#include <cv.h>

#endif
Glenn Teitelbaum
  • 10,108
  • 3
  • 36
  • 80
0

You shouldn't have equals signs or semicolons with #defines. What do you plan on putting in your Settings class?

  • To comment, use `add comment` under the question. This area is only for answers. Please delete this post. –  Nov 17 '13 at 21:48
  • 1
    @Desolator You need a certain amount of reputation in order to post comments. –  Nov 17 '13 at 21:49