Suppose we have below code:
class A {
uint32_t X;
uint32_t Y;
};
int main ()
{
A a;
uint64_t num = (uint64_t)a;
}
The compiler gives error: "Cannot convert from A to uint64_t. No User define conversion operator defined."
Is the error expected and if yes, why?
You haven't specified the 'endianness' of your representation. If you want
x
to be the high word andy
the low word, you could add a cast operator to your class:A
'(uint64_t) obj'
or (ideally)'static_cast<uint64_t>(obj)'
will call this method.explicit
prevents it being called implicitly - which is probably the dumbest thing I've said all day - but it prevents surprises. Extra points if you use<cstdint>
andstd::uint64_t
.