[alicebot-developer] ChatterBean 00.007 is now available

Helio Perroni Filho xperroni at yahoo.com
Sat Dec 10 11:31:37 PST 2005


--- mehri <foreverlinux at yahoo.com> escreveu:

> //Boy I wish C++ had reflection :-(
> Tag &createTag(String className)
> {
>    if(className == "category")
>       return new Category;
>    else if(className == "date")
>       return new Date;
>    else if // ... so on
> 
> }

Have you tried to use function pointers? For every
tag, you would have a function responsible or
returning a new instance of a given class, and then
you could have a map collection mapping tag names to
function pointers:

// Include the library for map templates.
#include <map>

// ...

/* Function pointer type for a tag-maker function. */
typedef Tag& (*TagMaker)(void);

/* Name-to-maker-function map. Must be filled
somewhere else. */
map<String, TagMaker> makers;

// ...

Tag &createTag(String className)
{
  /* Gets the function pointer from the map. */
  TagMaker maker = makers[className];

  return maker();
}

This might seem more complicated than Java reflection,
but it is feasible, and for a lenghty set of possible
tags, much more efficient than an if-then-else chain
-- retrieving a value from the map takes a time of
O(1) in assintotic notation, while going down a
conditional chain takes O(n) in the worst case, where
n is the length of the chain.

-- 
Ja mata ne.
Helio Perroni Filho



	



	
		
_______________________________________________________ 
Yahoo! doce lar. Faça do Yahoo! sua homepage. 
http://br.yahoo.com/homepageset.html 



More information about the alicebot-developer mailing list