Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

RTTI question

Name: Anonymous 2006-01-19 14:12

I've got this problem and I'm not sure how to solve it. I have:

class A {
    public:
        A() {};
        ~A{} {};

        void saveToXML(FILE *out);
        void loadFromXML(xmlNodePtr node);
};

Derived from A, I have several classes that do not reimplement savToXML() because all the member data is the same.  The only reason I have derived classes in the first place is to reimplement various member functions.

Obivously, when I'm ready to read the data back in from a file I do something similar to the following:

A *in = new A();
in->loadFromXML(node);

Of course, this results in me losing my polymorphism after the data structure has been saved and read back in.

So the question is this.  Is there any way to identify exactly what class saved the data in the first place and downcast a pointer to that type when reading the data back in without having to overload loadFromXML() in every derived class?  We're talking about hundreds of derived classes here, so I really don't want to do that.

Thanks!

Name: Anonymous 2006-01-19 17:44

If I understand you correctly, no, not really.  You've got a similar issue whenever you're serializing different types to some object-independent medium and then pulling them back.  I run into it more often over a network cable, but the core issue is pretty similar. 

I've typically dealt with it in a cheating fashion by storing my "type" as some sort of enum within the base class.  Each derived class has a different enum'd value indicating what "real" type it is so that I reconstruct the message as the appropriate type on the receiving end.  I've never used that solution with hundreds of derived classes either, though--- rather, at most, with 20 or so message types.

Nor do I claim it to be the best, or even necessarily a "good", solution.  I *am* interested in what other folks have done though.

Name: Anonymous 2006-01-19 18:58

Well, I've decided to solve the issue with a class factory. 

Basically I'll have a global map of all my types.  In the base class I'll have a createInstance() function that takes the class name as a parameter.

Then using typeid().name() I'll match up the class and return a new instance of it.

Thanks :)

Name: Anonymous 2006-01-20 0:40

I have a question. Since this thread seems to be solved.
How does one manage to get typeid().name()
return the EXACT class name defined by the programmer?

Coz I'm running into trouble where typeid().name()
gives me an unnecassay looooooooong name
ubtil the OS had to even reject it
when I try to use it to identify my window class.

Btw, I'm not the original author.

Name: Anonymous 2006-01-20 0:45

>>4
Same guy. Just to clarify.
I'm looking for any solution to overcome
this unnecassary long name.
Perhaps a special macro
or even a documentation to explain what all those cryptic symbols represent inside the string given by typeid().name()

Name: Anonymous 2006-01-30 21:58

>>4
You get mangled names from typeid().name(). An implementation-specific format actually. Quit complaining, that's the extent of reflection you get in C++.

Name: Anonymous 2009-11-02 15:28

ARISE, ANCIENT ONE!

Name: Anonymous 2011-08-18 15:29

ARISE, ANCIENT ONE!

Name: Anonymous 2011-08-18 15:52

QUACK!

Name: Anonymous 2011-08-18 21:07

< LOOK AT THEM!

Name: chanel bags 2011-08-19 8:49

Name: Anonymous 2011-08-19 9:18

CHANEL MY ANUS

Name: Anonymous 2011-08-19 12:15

I think I just got a bit dumber after reading this crap.

Name: Anonymous 2011-08-19 13:50

camelbags

Name: Anonymous 2011-08-19 14:39

I for one welcome our Lisper overlords.

Name: Anonymous 2011-08-19 14:48

>>16
fuck you lithpfag

Name: doudoune moncler 2011-11-09 22:40


Don't change these.
Name: Email:
Entire Thread Thread List