Page 1 of 1

casting a class to a string and vice versa

Posted: Thu Aug 07, 2008 12:04 am
by irruser
my current code is

Code: Select all

struct Data {
	int i;
	int y;
	Data(){i,y=0;}
};

	char * chr ="";

	Data * data;

	chr = (char *)&data;

	Data * d = new Data();

	d->i = 0;

	printf("%s\n",chr);

	d = reinterpret_cast<Data*>(&chr);

	printf("I = %i\n",d->i); // error here
when i print the d->i , i get strange values. is there any other way to do this?

Posted: Thu Aug 07, 2008 1:28 am
by vitek
You are not casting a class to a string at all. Your code dangerous at best.
is there any other way to do this?
I think a better question would be... what are you trying to do? I'm sorry, but there are so many things wrong with that code I don't even know where to start.

Travis

Posted: Thu Aug 07, 2008 1:51 am
by irruser
:? im trying to convert the class Data to a char array so i can send it or write it to a file then convert it back to class Data for modifying.

Posted: Thu Aug 07, 2008 6:21 am
by Dark_Kilauea
... Why?


Nobody told you about serialization, did they?

Save the data into a file, with a field showing what class it is, then on load, create a new instance of the class with the saved data.

You cannot cast a class to a char* and expect to get anything useful. Plus I highly doubt you will ever get that to load correctly on any other machine, or even your own machine after you close and restart the application.

Posted: Thu Aug 07, 2008 6:34 am
by vitek
It sounds like he is trying to serialize the object, just in a very primitive way. Converting to a string is not the answer. There will be all sorts of problems because the first null byte would terminate the string.

If your objects are pod (plain old data), then you can just write them out (using fwrite() or send()), making sure to handle endian issues as necessary.

You should probably take a look at the scene node serialization code (ISceneNode::serializeAttributes() and its ilk) which use xml.

Travis

Posted: Thu Aug 07, 2008 6:42 am
by BlindSide
vitek wrote:there are so many things wrong with that code I don't even know where to start.
Ditto,

Code: Select all

d = reinterpret_cast<Data*>(&chr); 
Here you are casting a char** (Note the double pointer!) that eventually leads to another double pointer (Data**), that eventually leads to an uninitialized Data pointer! In all honesty, simply keeping track of all the absurdities is giving me a headache...

How about we start from scratch?

Code: Select all

struct Data 
{
   int i;
   int y;
   Data() : i(0), y(0){} // Use a member initialization list here, it's a little faster. (Although for primitive data types like integers that's a moot point.)
};

   // Put the class in a char buffer.
   char* buffer = new char[sizeof(Data)];
   Data* data = new Data();
   memcpy(buffer, data, sizeof(Data));

   // Read it back.
   Data* anotherData = new Data();
   memcpy(anotherData, buffer, sizeof(Data));

   printf("I = %d\n",anotherData->i);

   // DELETE EVERYTHING!!!
   delete data;
   delete anotherData;
   delete [] buffer; // Make sure to use [] here!, since it is an array.
Cheers, this should do the trick...

EDIT: Posted at the same time as Vitek!, I fully agree with what he's saying, simply putting them in a string isn't doing anything useful, since you have to copy the memory to the file/binary socket whatever eventually anyway, it's better to do it in one go. Eg. fwrite(data, sizeof(Data), 1, file);

For converting between endianness check out htonl() and ntohl() (Host to net long and net to host long.).

Posted: Thu Aug 07, 2008 8:54 am
by torleif

Code: Select all

core::string<char> myString = "Hello world!\n"
People seem to forget that the hard parts of programming have been taken care of

If this is a learning exercise, I apologize, and recommend you experiment more. it's the best way of learning

Posted: Thu Aug 07, 2008 12:56 pm
by irruser
thanks very much :D . and sorry for the very bad coding I'm still a noob :oops: .