Topic: SV: implicit class std::object


Author: "Bo Persson" <bop@malmo.mail.telia.com>
Date: 1999/08/02
Raw View
Stan Brown <brahms@mindspring.com> skrev i
diskussionsgruppsmeddelandet:MPG.120d26b7a06ba2f4989df6@news.mindspring.com.
...
>
> I'm sorry, I seem to have a mental block in my POD, er head. I've never
> really grasped the POD concept, so I hope you'll forgive what are
> probably some stupid follow-up questions. (I checked Stroustrup3, but
> there are no entries "POD" in the index.)
>
> darin@bentspoon.com (Darin Adler) wrote in comp.std.c++:
> >Objects of POD type can include pointers to built-in types and other POD
> >structs. However, memcpy can be used to copy them. The standard
specifically
> >says that you can use memcpy.
>
> Yes, I understand that you can use memcpy on an object that contains an
> ordinary pointer, but in practice you never would because then you'd have
> two pointers pointing to the same object, right?
>
> Looked at another way, what kind of object is there that you could *not*
> copy using memcpy? Granted, with an object that has a virtual function
> you're also copying the vtable (at least in MSVC++ 5.0), but is there
> some reason why that's a problem conceptually?

Many (most?) implementations use a vtable, but that is not required.
Conceptually there might be a better way to implement virtual functions.

An object might also contain other information, like type info or some debug
structure that is unique for each object.

> >    3.9/2 states that copying the bytes of an object of POD type to an
array
> >of char and back with memcpy will restore the original value of the
object.
>
> I'm sure I'm missing something obvious, but I just can't picture any
> object of which that would not be true. Can you give a simple example?
>
> >    3.9/3 states that you can copy the value one object of POD type into
> >another object of the same POD type with memcpy.
>
> And here again, I'm lost. a, What object is there that you actually
> cannot copy with memcpy? but 2, why does this matter since memcpy is the
> wrong way to copy anything that contains a pointer?
>

You cannot use memcopy on a class that uses reference counting, like string
or MSVC CString. If you memcopy one of these, you increase the number of
objects pointing to a shared memory block without increasing the count. Not
good at all... :-)


Bo Persson
bop@malmo.mail.telia.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]