sizeof on a class

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!)?

Regards,
Ray L.

I think I've answered my own question. It appears &SomeInstance points directly to the data segment for the object, so using:

memcpy(Buffer, SomeInstance, sizeof(SomeClass));

appears as though it should work just fine.

Regards,
Ray L.

...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 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.

RayLivingston:
How do I get a pointer to the "first member data"? Is it guaranteed that will be the first declared data item?

Take a pointer to the class instance itself.

Foo bar;

send_some_bytes(&bar, sizeof(bar));

Some casting may be required to get that pointer treated as being a pointer to byte

send_some_bytes((byte*) (void*) &bar, sizeof(bar));

SomeClass SomeInstance = SomeClass();

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.

Much simpler.