At a previous employer, we were writing binary messages that had to go "over the wire" to other computers. Each message had a standard header something like:
class Header
{
int type;
int payloadLength;
};
All of the data was contiguous (header, immediately followed by data). We wanted to get to the payload given that we had a pointer to a header. Traditionally, you might say something like:
char* Header::GetPayload()
{
return ((char*) &payloadLength) + sizeof(payloadLength);
}
or even:
char* Header::GetPayload()
{
return ((char*) this) + sizeof(Header);
}
That seemed kind of verbose, so I came up with:
char* Header::GetPayload()
{
return (char*) &this[1];
}
It seems rather disturbing at first, possibly too odd to use -- but very compact. There was a lot of debate on whether it was brilliant or an abomination.
So which is it - Crime against coding, or nice solution? Have you ever had a similar trade-off?
-Update:
We did try the zero sized array, but at the time, compilers gave warnings. We eventually went to the inhertited technique: Message derives from Header. It works great in practice, but in priciple you are saying a message IsA Header - which seems a little awkward.