Re: Deleting objects obtained from a STL library

From: Jeff Flinn (NONONE_at_nowhere.com)
Date: 08/06/04


Date: Fri, 6 Aug 2004 08:21:45 -0400


"Aguilar, James" <jfa1@cec.NOBOTSwustl.edu> wrote in message
news:ceusui$9fc$1@newsreader.wustl.edu...
>
> "Aguilar, James" <jfa1@cec.NOBOTSwustl.edu> wrote in message
> news:ceurna$934$1@newsreader.wustl.edu...
> > Hey guys. A new question:
> >
> Are the objects in the container the same objects as the ones that I used
to
> fill the container? Are they references to those objects, or are they

No, STL containers hold copies of the values you inserted into them. You can
not insert references into a container.

> copies of those objects? How did the container know how to make the
copies?

They use the copy constructor for the type passed.

> Are they just shallow copies of the original objects (i.e. all pointers
are
> the same, all other members were copied completely)?

That's up to you when you implement the copy constructor. If you don't
supply one, the compiler provided default is a member wise copy. ie: invokes
the copy constructor of each memeber.

> Secondly, suppose I have a function. There is a type called Object which
I
> use here:
>
> Object& fun1()
> {
> Object a;
> return a;
> }
>
> is not legal but

Because upon exit of fun1 a is destroyed, so you end up with a reference to
a destroyed object.

> Object fun1()
> {
> Object a;
> return a;
> }
>
> is. I think I understand why. However, let me ask you this: is this code

You end up with an Object that is a copy of 'a'.

> going to be inefficient if "Object" is ten kilobytes in size? Is what's
> actually happening in the second example is that the entire object is
being
> copied and returned? Lastly, if this is so, can this overhead be avoided
by

Maybe so, maybe not. Many compilers provide RVO and/or NRVO ( named return
value optimization ). Which would elide the copy. Hence, the rule - avoid
premature optimization.

> only passing ints and assigning all medium or large sized objects to the
> free store?

As another poster suggested, use boost::shared_ptr:

typedef boost::shared_ptr<Object> tObjPtr;

> tObjPtr fun1()
> {
> return tObjPtr( new Object );
> }

> Lastly, is there anything I can do with references that I can't do with
> pointers? Is there anything that is especially inelegant with pointers
that

References can't be reseated, and must be initialized.

[...]

> I guess my question is, on which side should I err: using the free store a
> lot and using pointers a lot, or using references a lot and perhaps paying
> overhead when I return large objects from methods?

You should really get a good C++ book and study the FAQ.

Jeff F



Relevant Pages

  • Re: Deleting objects obtained from a STL library
    ... >> I want to use an STL libarary to hold a bunch of objects I create. ... >> besides using pointers rather than references for the STL library? ... The container uses the copy constructor, if no copy c_tor is provided ...
    (comp.lang.cpp)
  • Re: how to pass this before supertype constructor has been called
    ... >>whose constructor failed and whose memory has actually been freed and is no ... Java does not have pointers. ... It has references, ... The situation with Java is a bit different, ...
    (comp.lang.java.programmer)
  • Re: "Russian doll" class
    ... In .Net they have a routine to release references, ... Container collection and so on. ... A box's parent could be a container ... or a pallet which means a box might ...
    (microsoft.public.vb.general.discussion)
  • Re: STL-container used with references
    ... On 2004-04-06, Severin Ecker wrote: ... You cannot have a container of references, but you very well can have ... a container of pointers, i.e. something like std::vector. ...
    (comp.lang.cpp)
  • Re: how to pass this before supertype constructor has been called
    ... >>Java does not have pointers. ... It has references, ... the semantics of Java then don't write in Java. ... Furthermore, whether or not you use it in the constructor today, the ...
    (comp.lang.java.programmer)