I have a function so save the structure to byte array.
template <class TTput_stream>
1 2 3
Save (TTput_stream value) {
constchar* buffer;
buffer = reinterpret_cast<constchar*> (&value);};
It fails, no information is written to the buffer.
my_title has correct information (I see it on debugger view )
(All my others structures works fine )
As you can see , I have a type template for this function, so I'd not want to write an individual function for every class that has chars into them.
I need your help.
How are you using your buffer after this code?
Maybe your current code is treating 0 bytes as '\0' (end of strings), so you are getting an empty result when the first byte of your structure is 0
It depends on the type you are using as TTput_stream and the reason you try to convert the address of value instead of reinterpreting value itself. Most likely the problem is the address of operator because value receives by value, and therefore &value is just the address to the argument copy and not the original argument.
I am passing Myclas->structure to save function, so TTputstream is 'structure' .
This function works fine with my other structures.
Maybe the problem can be the function at I fill the my_title information ?
Ok, so &value is then OK. What do you do to actually save? Because the Save function you show only declares a pointer and then assigns to it the address of value. What else does it do?
Is it possible that is not possible to call set_text from the instance of 'structure'
That is to say :
I have an_instance_of_structure
And I have an_instance_of_structure.set_text("hello");
By any reason it is the origin of my problem.
However, strncpy(an_instance_of_structure,txt.c_str(),200) ; works fine.
Any idea ?
I am now very confused as to what you want to accomplish. Initially I thought you had some data inside an instance of structure that you wanted to save elsewhere, like a stream (file, memory, etc.).
Now with your last post it seems that you want the exact opposite: Set the instance of structure to a specifc value.
I would appreciate clarification on exactly what you want to do.
BTW, I have the feeling that your problem is better handled in a C++ way (classes + polymorphism) than the current approach. Your current approach, even when using templates, is very C-like, messing around with forced pointer casts.