template<typename T>
char * my_fun(T t)
{
union my_enum
{
T tx;
char buf[500];
}e;
memset(e.buf,0,sizeof(e.buf));
e.tx = t;
return e.buf;
}
class myc
{
public:
int a;
long b;
};
void main()
{
myc a;
a.a = 55;
a.b = 5545;
char buf[100] = "";
strcpy(buf,my_fun<myc>(a));
//what will buffer contains ?
}
will buffer contain object a in char buffer format.
(just like serialization ? ).
& will i able to get object back from that buffer ?
i am confuse ?
any idea ?
(sorry i don't know whether it is a proper question ?
or i am trying something wrong )
The first bytes of buf would contain the internal representation of a. Would contain because your example won't work - in my_fun you're returning a pointer to the buf array which is destroyed when the function exits.
Note that copying objects around like this only works with PODs.
template<typename src,typename des>
des my_cast(src s)
{
union
{
des d;
src ls;
}e;
e.ls = s;
return e.d;
}
char buff[100] = "";
template<typename T>
void my_ser(T ta)
{
union
{
T src;
char lbuf[100];
}e;
memset(e.lbuf,0,100);
e.src = ta;
memcpy(buff,e.lbuf,100);
}
class aclass
{
public:
int x;
long y;
};
void main()
{
aclass ia;
ia.x = 20;
ia.y = 35;
my_ser<aclass>(ia);
cout<<(my_cast<char *,aclass>(buff)).x; //here i expect value of x again but i did'nt
//get it ?
}
Why? Because the <src> template parameter is a pointer-to-char which happens to be buff (or &buff[0]).
You then typecast that pointer to be an <aclass>. aclass' first four bytes happen to be its <x> member
since aclass has no virtual functions, so x is actually the address of buff.
But what on earth are you trying to do? There is hardly a line of code there that isn't breaking the standard
in some way (even void main() breaks the standard).
The most general answer to your question is to use boost::serialization.
Anything else is going to have limitations that are hard to overcome. For example, what
to do if the class has pointers in it? Or, what to do if the class has virtual functions?
If all you have are simple classes/structs containing POD types and containing no
member functions, no destructor, and no constructor, then just use memcpy();
this is directly supported by the standard.
The moment you add a member function (virtual or not), a destructor (virtual or not), or
a constructor, the class is no longer a simple aggregate, and while memcpy() may work
in those cases, you are no longer standards compliant, and your code could break in
the future or with a different compiler.
The moment you add virtual methods your problem becomes much, much harder since
now the class has an implicit vtable pointer that, while you can store it to a file,
you cannot read it back later and expect the program to work at all.
Pointer data members are essentially the same problem as the vtable pointer problem,
in that you can't write the pointer to disk, read it back later, and expect those pointers
to point to anything useful.
vivmen, if you after all need to use an union, perhaps you can improve things a bit by using anonymous union {..};, but that's just a minor improvement...