Click here to Skip to main content
15,915,760 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Swap Bytes Pin
Aint2-Feb-07 2:00
Aint2-Feb-07 2:00 
GeneralRe: Swap Bytes Pin
Michael Dunn2-Feb-07 9:12
sitebuilderMichael Dunn2-Feb-07 9:12 
GeneralRe: Swap Bytes Pin
Christian Graus2-Feb-07 10:21
protectorChristian Graus2-Feb-07 10:21 
QuestionLoad a class depending on a string Pin
S.C.Wong1-Feb-07 22:57
S.C.Wong1-Feb-07 22:57 
AnswerRe: Load a class depending on a string Pin
Waldermort1-Feb-07 23:21
Waldermort1-Feb-07 23:21 
QuestionRe: Load a class depending on a string Pin
David Crow2-Feb-07 2:46
David Crow2-Feb-07 2:46 
AnswerRe: Load a class depending on a string Pin
jhwurmbach2-Feb-07 3:05
jhwurmbach2-Feb-07 3:05 
AnswerRe: Load a class depending on a string Pin
Cedric Moonen2-Feb-07 4:10
Cedric Moonen2-Feb-07 4:10 
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 Wink | ;) ).
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.


Cédric Moonen
Software developer

Charting control [v1.1]

GeneralRe: Load a class depending on a string Pin
Mark Salsbery2-Feb-07 5:53
Mark Salsbery2-Feb-07 5:53 
GeneralRe: Load a class depending on a string Pin
Cedric Moonen2-Feb-07 6:28
Cedric Moonen2-Feb-07 6:28 
GeneralRe: Load a class depending on a string Pin
Mark Salsbery2-Feb-07 7:11
Mark Salsbery2-Feb-07 7:11 
QuestionConvert LPWSTR to LPSTR Pin
ajitatif angajetor1-Feb-07 22:39
ajitatif angajetor1-Feb-07 22:39 
AnswerRe: Convert LPWSTR to LPSTR Pin
jhwurmbach1-Feb-07 22:46
jhwurmbach1-Feb-07 22:46 
QuestionCan load custom control with dll resource Pin
dungpapai1-Feb-07 22:36
dungpapai1-Feb-07 22:36 
Questionavailable characters Pin
vlad.gs1-Feb-07 22:19
vlad.gs1-Feb-07 22:19 
AnswerRe: available characters Pin
Naveen2-Feb-07 1:02
Naveen2-Feb-07 1:02 
GeneralRe: available characters Pin
vlad.gs2-Feb-07 1:44
vlad.gs2-Feb-07 1:44 
GeneralRe: available characters Pin
Hamid_RT14-Feb-07 6:15
Hamid_RT14-Feb-07 6:15 
AnswerRe: available characters Pin
Mark Salsbery2-Feb-07 6:41
Mark Salsbery2-Feb-07 6:41 
AnswerRe: available characters Pin
Mark Salsbery2-Feb-07 7:12
Mark Salsbery2-Feb-07 7:12 
Questioncan't get the updated data during Win32 Release Pin
MrKyaw1-Feb-07 22:11
MrKyaw1-Feb-07 22:11 
QuestionConversion Hex string to ASCII value Pin
Ram Murali1-Feb-07 22:00
Ram Murali1-Feb-07 22:00 
AnswerRe: Conversion Hex string to ASCII value Pin
Roger Stoltz1-Feb-07 22:18
Roger Stoltz1-Feb-07 22:18 
GeneralRe: Conversion Hex string to ASCII value Pin
Ram Murali1-Feb-07 22:39
Ram Murali1-Feb-07 22:39 
AnswerRe: Conversion Hex string to ASCII value Pin
Roger Stoltz1-Feb-07 23:29
Roger Stoltz1-Feb-07 23:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.