# Reference to a class type?

This topic is 2910 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Is something like this somehow possible?

(Pseudo code, ofc)
MyClass* Test;ClassType ct=GetClassType(Test);Write to file (&ct);//...//LaterClassType ct;ReadFromFile(&ct); //Read it from the fileMyClass* Test = new ct;

For now I have rolled my own solution, but I did this for my bigger project. And I don't want to do this for my little mario-clone, too.

##### Share on other sites
C++ doesn't provide anything like that out of the box, but if you overload the stream insertion and extraction operators then it would work just like you would expect. file >> obj and file << obj.

In fact Haskell is the only language that I know of that provides that feature out of the box.

##### Share on other sites
And UnrealScript does (Of course this is just a scripting language). This is where I come from, so I thought it would be great if C++ can do that too. My solution for this in my bigger project goes like that:

I made a tool which creates my classes for me. That tool creates both, .h and .cpp files with the name you give to it. And it also writes the name of the class in UPPERCASE as a const int into a special header file, called AllClasses.h. The class-maker also writes something into a function in AllClasses.cpp which takes such a number as input and uses a switch/case to check which object it should create. Then it creates it and returns it. That works pretty good so far. :) The only bad thing is that all those classes have to extend one base class, because of the creation function. But except that it works good for me. :)

But although it was a bit messy to write it...

##### Share on other sites
This is encompassed under something known as "reflection" and all .NET languages provide this functionality out-of-the-box via System.Reflection. C++ doesn't have this functionality natively, however if you check out something like the protobuf library you can examine one possible approach. They basically auto-generate their reflection code for each object like you do, and it works pretty well.

##### Share on other sites
For saving that class name you could do

MyClass* Test;
std::string classType = typeid(*Test).name();

Deserialization of the right class type might seem to be a little more tricky, but it isn't really if you grasp the concept.

I suggest this link, it provides some good info [36.8] How do I serialize objects that are part of an inheritance hierarchy and that don't contain pointers to other objects?

##### Share on other sites
It's probably bad form, but for something like this I usually just create a small Class ID for each class,
eg.

class SomeClass{   private:   unsigned int classID;   public:   SomeClass() {   this->classID = SOMECLASS_ID;  //set the ID in the constructor   }}

It comes in handy when you're working with many SomeClass-derived objects, I find.

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631428
• Total Posts
3000028
×