Click to See Complete Forum and Search --> : Strings to Objects


DeepT
November 15th, 2004, 04:09 PM
I am working with XML and have a problem. How do you convert an a string into an object?

What I have done is get the object tag (which tells me what object it is), and map it to a function (which takes an XML string containing the construction values) which then constructs the object. I have a table of string names and function pointers to call when it finds a match.

Is there a better way to do this?

Marc G
November 16th, 2004, 03:07 AM
That is correct. It's almost the same as the well know Factory pattern.

ryadav
December 21st, 2004, 09:42 AM
Marg if we consider it as Factory, then probably implementation will be like this
please correct me if some thing wrong this pattern design for the posted question.

class Abstract {
virtual void Object () = 0;
};

class AObject : public Absrtact {
void Object (char *str) {
strcpy (ostr,str);
}
private:
char *ostr ;
};


class facotry {
virtual AObject* FObject () = 0 ;
};

class Concretefactory : public factory {
AObject* FObject () {
return new AObject ;
}

};

main () {
Concretefactory *cf1 ;
AObject aobj;
aobj = cf1->Fobject () ;
aobj -> Object ("Raghu") ;
}

Andreas Masur
December 21st, 2004, 09:49 AM
Abstract Factory (http://home.earthlink.net/~huston2/dp/factory.html)
Factory Method (http://home.earthlink.net/~huston2/dp/factoryMethod.html)