|
|
Darren New <dne### [at] sanrrcom> wrote:
> Warp wrote:
> > You can't access the private variable by address. Not according to the
> > standard.
> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf
> Section 3.9 paragraph 2, page 60 sheet 74.
> #define N sizeof(T)
> char buf[N];
> T obj ; // obj initialized to its original value
> std::memcpy(buf, &obj, N); // between these two calls to std::memcpy,
> // obj might be modified
> std::memcpy(&obj, buf, N); // at this point, each subobject of obj of scalar
> type
> // holds its original value
> Yet I'm sure at this point that there will be some reason this doesn't count
> as violating modularity. Will it be that it's only a draft of the standard?
> Will it be that it's only true of POD types? Will it be because it only says
> it works for reading and writing all the private variables at once?
By the same logic this would also "break modularity":
T obj;
T obj2 = obj;
All the data in obj is copied to obj2. This includes all the private data.
However, the memcpy() version *is* breaking modularity because, as the
text says, it's guaranteed to work only if the object contains scalar
types. If you assume it does, you are breaking modularity.
--
- Warp
Post a reply to this message
|
|