Hi everyone :)

At the minute I'm working on some stuff learning c++ and was wondering if ayone could offer some advice...

Say I have a pile of objects that need to be created (for example say, a Log object, XML parsers object, and a command line parser object). In my main.cpp file I have a pile of code setting up each of these objects, then I add a pointer to these objects to another list (called object manager) so I can pass this list to other parts of the program and get access to the previous mentioned objects.

Is there a way of creating say a separate file (say LogInit.cpp, XMLParserInit.cpp) that contains all the initalision stuff, and have these file called or run in main.cpp? So that way a user shouldn't have to modify or change main.cpp for a new object or to change stuff, only edit/make a new *object*Init.cpp file?

Haven't tried much yet, only messed about with a few idas, such as having a InitObject object that could maybe contain a Init method, then say logInit.cpp extends this InitObject class and overrides the Init method?


Sorry if this makes no sense, feel free to tell me it's a crazy idea if ya want! :P

It's a little bit vague, but I think it depends exactly on what your code currently does and how much those objects have got in common (more importantly, how much your initialisation code has got in common)

Are you currently implementing this using a lot of repeated code? If so, then there will almost certainly be some way of trimming down the extra "fat". Do you have an example of the way your objects are currently created/initialised?


If you're looking at any common code which exists between different class types, then what you're looking for might be solvable using template function(s), or building a relationship using inheritance, or some other design pattern.

Well at the minute I have stuff like this in my main.cpp

std::string logName = "";
std::string logDir = "";
bool debugMode = false; 

configManager->getParameter ("LogName", logName);
configManager->getParameter ("LogDir", logDir);
configManager->getParameter ("DebugMode", debugMode);

Log* myLogger = new Log (logName, logDir, debugMode);

objectManager->addObject <Log> (myLogger, "Logger");

so I was wondering is there a way of placing the above code in say a separate file (InitLog.cpp), and calling this code somehow? so main.cpp could be like

/* configManager to get command line arguments etc...
 * objectmanager to store the pointers to objects
 */
InitObjects* initObjects = new initObjects (configManager, objectManager);
initObjects->initAllObjects ();

And it would somehow call all the code in InitLog.cpp, add the pointer to the objectManager list and so on, instead of clogging up the main.cpp with tons of code?

You could try to make a separate initialization file, but then your main.cpp file will have to include a define the variables as extern. So it would look something like this:

initialize.cpp

int blah=4;
int bloo=5;
float foo=2.4;

and then main.cpp would be something like

int main() {
   extern int blah;
   extern int bloo;
   extern float foo;
}

Note that this way gives you some leeway about the initialization, but you still have to define each and every variable as extern (external). I don't know if that's the most elegant solution so let the other gurus give their 5 cents :-)

-Ruslan

I would strongly suggest that you avoid using extern variables (They are global variables). If you're not familiar with problems associated with them then have a quick look on google www.google.co.uk/search?hl=en&safe=off&q=why+are+global+variables+bad


As to your problem - if all you're after doing is cleaning up main() and dumping some extra code elsewhere, then a simple function would seem to do the trick. (Or am I misunderstanding/oversimplifying it?)

Initialisation.h

#ifndef INITIALISATION_H
#define INITIALISATION_H

class ConfigManager;
class ObjectManager;

void InitObjects( ConfigManager& configManager, ObjectManager& objectManager );

#endif

It would certainly seem the simplest solution which should be quick to implement. (It doesn't particularly change or simplify your code, but it will take that code out of main)

Adding the .h and .cpp files to your existing project depends on the IDE you're using. In most popular IDEs, all you need to do is add a source and a header file to your project, the rest should be handled automatically - You'll need to #include the .h file from your main cpp file though, just like you do with iostream/string/etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.