Thanks.
I can't use std::string in my structure ? SIzeof does not understand the size of the string. This is the reason to use char, but I think that using c++ there must be another aproach.
Any idea ?
Thanks
Uf.
Oh my friend I want to have my std::string into a structure and I want to know the size of the structure.
I have seen that it is imposible to use std::string inside a structure if you are going to load_save this structure. ( i think ... )
Thanks.
#include <iostream>
#include <string>
usingnamespace std;
struct My_structure
{
string title;
float x;
int y;
};
int main ()
{
My_structure a;
a.title = "this is title for struct a";
cout << "The size of str in struct is " << a.title.size() << " characters.\n";
cout << "Size of the struct is " << sizeof(My_structure) << endl;
cout << "Max size for string inside struct is " << a.title.max_size() << endl;
return 0;
}
After running i get following output:
The size of str in struct is 26 characters.
Size of the struct is 12
Max size for string inside struct is 1073741820
Yes, as you can see using std::string makes impossible to get the 26+4 bytes I need to convert my_structure to byte array (that it is what I want to do )
If you need the struct object ptr to be converted to a byte array, then yes you need it to be POD. it is no longer POD the moment that you had a std::string variable. However, you can learn to serialize by overloading the stream operators. That would be more of an object oriented approach. Do a google search on "c++ serialization", if you want to learn about other ways.
If you want a generic function so save to a byte array any class using templates, you cannot use std:string inside your classes, because inside this generic type template function you cannot know the real size of the class you want to store.
That is to say, if you want to use std::string into a class, you need a custom function to can compute the size of the string and so, can to know the right number of bytes of your instance.
So, you have to use char[] and strcpy the std::string to the char . Its a pitty, because then you need to include stdio.h.
Tonnot, you really should take kempofighter's advice and do some Googling about serialization.
Are you trying to persist your class? Are you trying to stream it? In either case, you need to either know the size of your object, or the end of your stream.
If I were trying to persist this class, I would probably add a size_t to the structure to contain the string's length:
struct My_structure
{
size_t stTitleLength;
string title;
float x;
int y;
};
The do something like this:
My_structure s;
s.title = "Persist me!";
s.stTitleLength = s.title.length();
size_t ThisMuch = sizeof( s ) + s.stTitleLength;
ofstream Out( "something.out" );
if( Out.good() )
Out << s.stTitleLength << s.title.data() << s.float << s.y;
This will make it easy to read it back in. There are very many ways to dothis.
You are trying to use a generic function the wrong way. They weren't meant to solve what you are trying to do. The standard streams are meant to do it, so use them.