Archived

This topic is now archived and is closed to further replies.

Mupp

I know im bad at organising my code, but this is some serious shit :(

Recommended Posts

Okay, i dont think im lying when im saying that i might be the WORST code organizer out there, ever! Im trying to make an "ground"standing for a future engine, im not sure now if it will ever be one or if im just.. playing around testing my skills.. the problem is.. I have allot of classes.. (im using C++ and SDL) and i have one class for the handeling of window initialisation, i know it sounds like bull maybe, but it might be usefull in bigger stuff, or something ^^ and then, i got a class for my logger! now, in a "main" file, i include all these files, window initialiser, logger and so on.. but in my window initialiser code, i want to also be able to use a logger.. to the same code( i dont want to create two objects of one logger, but use the SAME object ( same logger)) in a different .h / .cpp file.. does it make sense? if so, how can i do this?

Share this post


Link to post
Share on other sites
You might want to check out a singleton class (only one instance of the class can be created). Below is an example:

CLogger.h

#include <string>
#include <fstream>

class CLogger {
public:
static CLogger *getInstance();
static void killInstance();

void openLog(std::string File);
void closeLog();
void writeLog(std::string Message);

private:
CLogger() { };
~CLogger() { };

static CLogger *Instance;
std::fstream Stream;
};



YourClass.h
   
#include "CLogger.h"

class YourClass {
public:
YourClass();
~YourClass();

private:
CLogger *Logger;
};


YourClass.cpp
   
#include "YourClass.h"

YourClass::YourClass() {
Logger = CLogger::getInstance();
Logger->openLog("Debugfile.txt");
Logger->writeLog("Someinfo");
}

YourClass::~YourClass() {
Logger->closeLog();
Logger = NULL;

CLogger::killInstance();
}


You wouldn't need to use killInstance until you're completely done with the logger, like at the end of the program.


[edited by - Dustino on March 29, 2004 3:16:50 PM]

Share this post


Link to post
Share on other sites
But i also want to be able to use it more than one file? like, i want my logger to be "all over the place" in my init file, in my main file, in my graphics file, but still be the same logger. you know?

Share this post


Link to post
Share on other sites
I''m sorry I think I''m misunderstanding your english a bit..

But if you want more classes to be able to use the logger just add a class pointer to CLogger in whatever class you want like what was done in YourClass.h above... and use the getInstance method above to get the instance of the logger.

Share this post


Link to post
Share on other sites
i think he wants to be able to use an instance of the class in ALL of his files, and have it be the same instance. you want to use the extern keyword for this. it makes a variable (or object) global to ALL your cpp files. just do this:

//in your main, or wherever its declare
Logger mylogger;

//now in ANY OF the files that you want to use mylogger
extern Logger mylogger;

if this doesnt make sence, i had just posted a thread where i was having problems trying to do this. just scroll down and look for "how do i make a object global to ALL files?". it should help you out.

Share this post


Link to post
Share on other sites
Mupp : with the given singleton example you can access the logger in any file since the logger holds a static reference to itself CLogger::getInstance() will always return the same instance of the logger, in any file. special problems arise when using the singleton pattern across process boundaries but that''s another matter

Share this post


Link to post
Share on other sites
Mupp, använd en pekare som är en global. Just use a pointer variable and create the instance of the class with "new". The pointer to your class will be a global variable. Then whenever you want, make the instance of your class using "new" and whenever and wherever you want to use it, use it.

(Det goer jag i alla fall i mina program och det fungerar utmärkt.)

Brian

Share this post


Link to post
Share on other sites
Use Singleton pattern. See online tutorials on this topic if you want to know more.

An exert of my Singleton:

template<typename T>
class CSingleton
{
static T* ms_singleton;
public:
CSingleton()
{
assert(!ms_singleton);
//use a cunning trick to get the singleton pointing to the start of the whole, rather than

//the start of the CSingleton part of the object

int offset = (int)(T*)1 - (int)(CSingleton <T>*)(T*)1;
ms_singleton = (T*)((int)this + offset);
}
~CSingleton()
{
assert(ms_singleton);
ms_singleton = 0;
}
static inline T& GetSingleton()
{
assert(ms_singleton);
return *ms_singleton;
}
static inline T* GetSingletonPtr()
{
//assert(ms_singleton);

return ms_singleton;
}
};


Good luck.

" Do we need us? "


Ionware Productions - Games and Game Tools Development

Share this post


Link to post
Share on other sites