On Mon, Jul 27, 2009 at 7:48 AM, Andrei Alexandrescu<seewebsiteforem...@erdani.org> wrote: > Leandro Lucarella wrote: >> >> Andrei Alexandrescu, el 27 de julio a las 07:59 me escribiste: >>>> >>>> For example, should the author of a container library prefer classes or >>>> structs? >>> >>> I've been struggling with this forever. I don't know. I don't even know >>> whether reference or value semantics are best for containers. I don't >>> know whether abstract container interfaces and container-independent >>> code are a net win; experience with STL seems to say "don't" and >>> experience with Java seems to say "ho-hum". >> >> About values vs. reference semantics, I think reference is the best. >> Containers usually are big, and you don't want to copy them arround. >> I find myself using lots of references to pass containers arround in C++ >> and almost never use the copy() method of Python containers, so based on >> *my* experience, I'd say that reference as the default is the best >> approach. > > Sounds convincing.
That's exactly right except for the times when it isn't and what you want is value semantics. I've just finished recently refactoring some C++ code that wasn't designed with copying in mind. Changing all the "float* data; int data_length;" members into std::vectors did the trick. The data they contained wasn't particularly large, and there was no real need to introduce the complexity that sharing it between copied instances would have created. --bb