Parameters and include files

Actually, scrap what I said yesterday. The common situation still is: you have a set of parameters that are basically global to the program. But, of course, it is not good to go totally global. OK, I think the best idea is to define a class in a header file. Then, declare one of these classes in the header file as external. Then declare (for real) just once somewhere. Ideally a singleton class should be used, but this is a bit over my head, just remember to declare one of these classes (and just once). (Unless you do want several sets of parameters, who knows…)

The members of the funtion are all public, and contain the parameters. Plain static members don’t make much sense (since there will only be one of these classes), but constant data should be declared static const, and declared somewhere (globally).

So, for example:

file “defs.h”

#include<iostream>

using std::endl;
using std::cout;

class dta {

double f;

static const int i;

};

extern dta data;

Then, define stuff somewhere:

file “defs.cpp”

#include”defs.h”

dta data;

const int dta::i=10;

void defs(void) {  data.f=0; }

Finally:

file “main.cpp”

#include”defs.h”

int main(void) {
defs();

cout << data.i << endl;
cout << data.f << endl;
cout << “Done” << endl;
return 1;
}

Just compile with g++ main.cpp defs.cpp.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s