|
There is no problem with your method of creating instances.
If I were to tackle the same problem, I would first convert the strkey into a DWORD value, then use a switch statement rather than comparing the string each time. Also, I would convert the strKey to lower-case before doing any comparison.
|
|
|
|
|
S.C.Wong wrote: When the list of classes getting longer and longer, the above way is too tedious and hard to maintain.
How so? Each additional animal requires 1-2 lines of code.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
S.C.Wong wrote: Something like:
if (strKey == "cat") m_pPet = new CCat();
else if (strKey == "dog") m_pPet = new CDog();
else if (strKey == "monkey") m_pPet = new CMonkey();
...
Personally, I would put this into a PetFactory -Class, which has a static method
Pet* CreatePet( const std::string& PetType);
With basically your logic inside.
Your call would look like this:
PetFactory factory;
m_pPet = factory.CreatePet( "cat");
Maybe the Petfactory would be a singelton or a member.
This way, You may later change the way the connection string<->pet-derived class is done. Maybe you want it configurable by a file?
Simply give the file to the PetFactory , change the PetFactory internals to build a map string<->c'tor and youre done.
"We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation."
-- Caius Petronius, Roman Consul, 66 A.D.m_PetFactory
|
|
|
|
|
You can use a factory pattern to that: create a factory class which is a singleton (if you don't know what the singleton design pattern is, I suggest you make a search on the web, you'll get better explanations that I can give you ).
This class provide a function that is able to create a class depending of a certain key (in your case, its name). Of courses, all classes must inherits from a common base class (animal in your case).
The factory in fact consist simply of a map in which each entry link a key (animal name) to a function pointer which can create a specific instance of your class (e.g. a dog).
So, the create function simply looks up in the map for the specific creation function, calls it and returns the newly created object.
Next, this factory needs to expose a way to add new entries in your map. This is done through a public function (called registerAnimal for example) that takes for arguments a key (the animal name) and a pointer to the creation function for this type of animal. This register function must return something (a boolean for example), I'll explain why later.
Ok, so far, your factory is ready to be used. So now, how can we register our animals in the factory in a generic way ? That simple: in the source file of your class, add a global static member of the type returned by your register function of your factory (e.g. a boolean):
static bool bRegistered = CFactory::getInstance()->registerObject("Dog",CreateDog);
CAnimal* CreateDog()
{
return new CDog;
}
These static variables will be evaluated when your program start, and will invoke the registerObject function from your factory supplying your CreateDog function. So now, every animal is registered with your factory.
If you create a new animal type, just add these lines of code in your cpp file and the class will be registered automatically through your factory.
Hope this helped.
|
|
|
|
|
Nice solution but now you have to add 4 lines of code instead of two every time a new animal
is added?
|
|
|
|
|
Yes, but your code is much more maintainable: you have no dependcy at all between the different classes and the 'factory'. The factory only knows the base class. You can even do better by using a template factory so that you have a completely generic factory.
And also, whenever you add a new class to your architecture, the only thing you have to change is the file in which you have your class (which must be created).
|
|
|
|
|
Cedric Moonen wrote: Yes, but your code is much more maintainable
Yeah Just teasing! You could actually implement macros similar to MFC's
DECLARE_DYNCREATE/IMPLEMENT_DYNCREATE to make maintenance really simple.
|
|
|
|
|
Hi everyone,
I need to convert a unicode LPWSTR to multi-byte LPSTR .wcstombs _s doesn't do good,because it replaces non-english characters to their english likes (like ş->s and ı>i).
WideCharToMultiByte function seems to do this right,but the double-byte characters in the converted LPSTR are interpreted as if they are two single-byte characters.
to be more specific,I use MAPI dll to send an e-mail.MAPI dll expects all strings to be LPSTR which maps to int .but i want to use non-english characters in e-mail's body/subject/attached files' names,and couldn't find a solution yet.it's all good when strings are in english,because all unicode characters map to a signed char then.but when i get a non-english character,"problem".LPSTR seems to interpret all characters to be single-byte.
any suggestions?
|
|
|
|
|
ajitatif angajetor wrote: MAPI dll expects all strings to be LPSTR which maps to int.but i want to use non-english characters in e-mail's body/subject/attached files' names,and couldn't find a solution yet
What you need is encoding.
There are several types of encoding around, some standards, some more habits grown out of prolonged use.
Read about MIME.
"We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation."
-- Caius Petronius, Roman Consul, 66 A.D.
|
|
|
|
|
I have to support French for an application.
All the resources of my application are stored in a DLL for French. when select default language, main resource is loaded and if select french, french dll of resource is loaded. Everything is fine as long as no custom controls are used.
I get a problem with custom control when load resource dll:
In old version, there is a control which managed by ActiveX Control, when i load resource dll (french) -> DONE:
CONTROL "",IDC_PREF_DIALOG_HIST,
"{E41EAFC4-F536-11CF-93A1-0000C0A61E5A}",WS_TABSTOP,7,7,
397,114
But in new version, this control is edit to Custom Control and manage by a new class (CHistogramCtl). when I use default language (English) => OK. But when I use french language (resource DLL), I could not load dialog which contain this control.
CONTROL "Custom2",IDC_PREF_DIALOG_HIST,"Histogram_Ctrl_Class",
WS_TABSTOP,7,7,397,118,WS_EX_CLIENTEDGE
Any suggestion will be helpful.
Thanks in advance,
Papais
|
|
|
|
|
Hi,
Does anybody know how to determine the unavailable characters(replaced by a square) from a font?
Thank You
|
|
|
|
|
how about GetTextExtentExPoint()?
nave
|
|
|
|
|
how could I use it? since the font replaces the "unavailable" characters with a square? it's size is about the same with the the others
|
|
|
|
|
|
Does isprint()/iswprint() help?
|
|
|
|
|
or maybe GetGlyphIndices()
|
|
|
|
|
Dear All,
I had a problem above this issue since last month. I can get the updated data of frequency value using NI-DAQmx card in Win32 Debug accurately. However, I already checked with Dependency Walker for necessary *.dll and traced line by line, I can't see any bug and error from my program.
I hope someone will give me some idea for that.
Thanks and best regards
|
|
|
|
|
Dear All,
I have some problem in converting Hex string to ASCII value. For example, I have Hex string of "50" and I would like to display the ASCII equivalent "P".
If i know the value to convert i can just use
CString str;
str = "\x50";
to convert to ASCII value. But the Hex value is entered by the user. So I dont know how to do that.
Can Anyone please help me in this?
Thanks in Advance.
Regards,
Ram
|
|
|
|
|
Would ::strtol() help you in this situation? Info here.[^]
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
|
|
|
|
|
Can you please give me an example?
Regards,
Ram
|
|
|
|
|
Ram Murali wrote: Can you please give me an example?
I provided a link in my previous post to MSDN.
Follow it and you will find another link in the MSDN article to an example.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
|
|
|
|
|
But this function is to convert str to long. how can i get a character?
Regards,
Ram
|
|
|
|
|
char cAsciivalue = nlong;// the long value u got
nave
|
|
|
|
|
Ram Murali wrote: But this function is to convert str to long.
Of course; and that return value is the ASCII code for the character.
E.g. if your user has input "50" the long return value of ::strtol() will be 0x50, which is 80 in decimal format, and this is the ASCII character code for 'P'.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
|
|
|
|
|
Thanks for your effort to solve my problem. Its working well. Thanks again.
Regards,
Ram
|
|
|
|