The use of define and singleton

4 posts / 0 new
Last post
workhard
workhard's picture
Offline
Mobile Conjurer
Joined: 3 May 2011
Posts:
The use of define and singleton

any ideas?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Sam Pickard
rival's picture
Offline
Mobile Archmage
Joined: 19 Mar 2009
Posts:

Any questions?

Do you mean what are 'define' and what is a singleton?

#define is a preprocessor instruction. Before compile any code, it will replace the first value with the second. For instance, if I've got:

#define NAME "Sam"

I can write:

lprintfln(NAME);

When it compiles the code, it will replace the NAME with "Sam" before compiling, so what it actually compiles is

lprintfln("Sam");

It's very useful when working with settings, because you can't mistype something.

A singleton is a design pattern. A singleton class only has one instance, and every consumer of that class uses the same instance. Typically, this is done by having a static method which will return a pointer or a reference to the instance.

workhard
workhard's picture
Offline
Mobile Conjurer
Joined: 3 May 2011
Posts:

thanks sam i understand the define part. but for singleton still not quite sure anyway to understand them?

Diego Velazquez
diegolv's picture
Offline
Mobile Wizard
Joined: 4 Oct 2010
Posts:

Hi,

check this:

http://en.wikipedia.org/wiki/Singleton_pattern

In fact, you don't need to use defines. Check this example:

 

class Globais {
	public:
		Globais();
		~Globais();
		static Globais& getInstance();
		static void disposeInstance();
		uint getUInt32(MAUtil::String param);
		double getDouble(MAUtil::String param);
		bool getBoolean(MAUtil::String param);
		void getString(MAUtil::String param, MAUtil::String& value);
		void setParam(MAUtil::String param, double dValue, MAUtil::String sValue);
	private:
		MAUtil::HashMap<MAUtil::String,MAUtil::String> mConfig;
		static Globais *instance;
		char* nullString;
};

Diego