1
如果我已將結構的成員複製到我的類中,我是否可以從我的類投射到結構中?標準佈局類型和reinterpret_cast
#include <stdint.h>
#include <sys/uio.h>
class Buffer
{
public:
void * address;
size_t size;
Buffer(void * address = nullptr, size_t size = 0)
: address(address), size(size)
{
}
operator iovec *() const
{
// Cast this to iovec. Should work because of standard layout?
return reinterpret_cast<iovec *>(this);
}
}
你爲什麼需要這門課? –
這絕對不是常量正確的。 –