need some comment - god or bad is ok

I am writing a piece of code, more specifically: a class for helping initializing variables inside a namespace for everytime you are including an external cpp file to your main code. I just tested my code for initializing a simple int variable, and it worked well atm.

Here is the code:
---------START : misc.cpp
#include "misc.hpp"
#include <cassert>
#include <cstdio>

class Init
{
public:
Init(void (*ctor)(void), void (*dtor)(void) = NULL )
: _ctor(ctor), _dtor(dtor)
{
assert(ctor!=NULL);
ctor(); // constructor
}

~Init()
{
if (_dtor!= NULL) _dtor(); // deconstruct
}
private:
void (*_ctor)();
void (*_dtor)();
};

namespace hsv
{
int somevar;
static void initialise(void)
{ somevar = 1; }
static void deinitialise(void)
{ somevar = 0; }

static Init foo(initialise, deinitialise);
//variable and value initialisation
};

--------END

main function just for testing it:
--------START: untitled.cpp
#include "misc.cpp"
#include <iostream>

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

int main ()
{
cout << hsv::somevar << endl;
return(0);
}
--------END

I'm using class instatiation to object in each namespace to initialise some value.
the reason I'm doing this is because I have a numbers of 2-dimensionals array that if computed in the main function through the formula for over repeated time would slow down my system significantly, and hence I want to be able to store those constant pre-calculated value into memory. and so with this I hope to be able to separate different arrays into different namespace, and initialise those arrays

With this kind of concept, can anybody comment if it's good to do this way or not? or can you also please if possible reccommend a better way?
thanks
[code] "Please use code tags" [/code]
You should not include sources (cpp), but headers (h). It will get you redefinition issues.

You could read the values from a file.
This is a bad idea.

1) It doesn't make any performance difference whether the code is in main or not. Putting it globally or in a separate ctor is just as slow/fast as it would be if you put it in main.

2) There's no point in writing a class that mimics construction/destruction of objects. You're better off just using constructors/destructors directly.

3) Including .cpp files will screw up the linker if you are building correctly, as ne555 said

4) Defining globals in a header like that will also screw up the linker if you are building correctly.

5) Because the 'Init foo' object is static, it will have an instance for each .cpp file that includes it. As a result, hsv::somevar will be initialized and deinitialized multiple times (yielding worse performance than just initializing it normally in main)


As for recommending a better way to do it, I'm not entirely sure what you're trying to do. If all you want is a global var that's initialized to 1, then the normal way is to do it like this:

1
2
3
4
5
6
// misc.hpp

namespace hsv
{
extern int somevar;  // note:  extern because it's global in a header
}

1
2
3
4
// misc.cpp
#include "misc.hpp"

int hsv::somevar = 1;  // instantiate it, and initialize it with 1 

1
2
3
4
5
6
7
8
9
10
11
12
// main.cpp
#include <iostream>
#include "misc.hpp"

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

int main()
{
    cout << hsv::somevar << endl;
    return 0;
}
Last edited on
Topic archived. No new replies allowed.