Just to clarifiy the reference vs pointer thing a very last time:
For the compiler a reference is the same as a pointer.
It just points to some address in memory where the data starts. Thats a simple fact!
But references have a lot of benefits:
- Automatic referencing and deferencing
- Less typing (No -> needed)
- const makes it more safe
- (Maybe more type checking)
- (Maybe magic optimizations by the compiler)
int value = 0;
int *ptrValue = &value; // Referencing is explicit
int &refValue = value; // Referencing is done automatically
*ptrValue = 10; // Deferencing is explicit
refValue = 42; // Deferencing is done automatically
// Return value is 42
return value;
Btw. my view about c++ has changed: I love it and wont code in C anymore ;-)
Now back to topic:
Why do you not do this?
- In this case, count are not needed i think
- Memcpy is not needed to done explicit
- Naming things explicitly to int32, int16, int64 whatever is much cleaner than just read_int, especially when you work with streams or network
- When you do more than reading, like converting or something like that you name it unpack_int32_from_stream
- There is no safety check here, you should include a maxStreamCount and check boundary
inline int32_t read_int32_from_stream(int &streamIndex, const char *streamSource, const int maxStreamCount)
{
assert((streamIndex + 4) <= maxStreamCount);
int32_t result = *(int32_t *)(uint8_t *)(streamSource + streamIndex);
streamIndex += sizeof(int32_t);
return result;
}