I've never really had a reason to care about this before, but now I do! It appears that doing a sizeof() on a class returns the size of the member data of the class, which makes sense. What I want to do is send a copy of the member data over a network to another identical device, to initialize another identical instance there. Presumably I can do this by copying sizeof() bytes, starting from the first member data item. How do I get a pointer to the "first member data"? Is it guaranteed that will be the first declared data item?
For example:
class SomeClass
{
public:
SomeClass() { SomeData = 1234; SomeOtherData = 2345; };
~SomeClass() {};
int SomeData;
int SomeOtherData;
}
SomeClass SomeInstance = SomeClass();
byte Buffer[sizeof(SomeClass)];
...
memcpy(Buffer, &SomeClass.SomeData, sizeof(SomeClass));
Will this work reliably? Is there a better way (I hope!)?
[quote author=Coding Badly date=1516137194 link=msg=3567130]
...or this for methods.
The only caveat occurs with multiple inheritance / interfaces.
Bear in mind, if there are any virtual methods, the first element is a pointer to the VMT.[/quote]
That does not surprise me. But the classes in question are very simple, no virtual methods, basically just structured data packets.
Regards,
Ray L.
This invokes the constructor of the class to create an instance of the class with no name. Then, it invokes the copy constructor to copy the instance to SomeInstance. Then, it invokes the destructor to delete the unnamed instance. Why would you want to do that?
SomeClass SomeInstance;
Invokes the constructor to create a named instance.