Jump to content
  • Advertisement
Sign in to follow this  
mind in a box

Reference to a class type?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is something like this somehow possible?

(Pseudo code, ofc)

MyClass* Test;

ClassType ct=GetClassType(Test);

Write to file (&ct);
//...

//Later
ClassType ct;
ReadFromFile(&ct); //Read it from the file

MyClass* 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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!