clarification on pointer use

Discussion in 'C++' started by Ruby Stevenson, Feb 22, 2012.

  1. All -

    I have a newbie question. Let me try to use an example to illustrate:
    I have some Event class defined, and I could save a collection of
    event objects in two ways:

    method 1:
    vector<Event*> event_vec;
    ....

    Event * event = new Event();
    event_vec.push_back(event);

    method 2:
    vector<Event> event_vec;
    ....
    Event event;
    event_vec.push_back(event)

    So one is store a pointer to event; the other store a copy of the
    event in a container.
    what are the implications between the two choices? when would be the
    proper use of one instead of the other?

    Thanks

    Ruby
     
    Ruby Stevenson, Feb 22, 2012
    #1
    1. Advertising

  2. On 2/22/2012 7:37 AM, Ruby Stevenson wrote:
    > I have a newbie question. Let me try to use an example to illustrate:
    > I have some Event class defined, and I could save a collection of
    > event objects in two ways:
    >
    > method 1:
    > vector<Event*> event_vec;
    > ....
    >
    > Event * event = new Event();
    > event_vec.push_back(event);
    >
    > method 2:
    > vector<Event> event_vec;
    > ....
    > Event event;
    > event_vec.push_back(event)
    >
    > So one is store a pointer to event; the other store a copy of the
    > event in a container.
    > what are the implications between the two choices? when would be the
    > proper use of one instead of the other?


    Implications are the same as storing a copy versus storing a pointer to
    the original object anywhere else. Changing the copy has no effect on
    the original object, so if you expect to pass it around, the changes
    have to be carefully tracked. Storing a pointer puts the responsibility
    of maintaining the lifetime on you, whereas a copy lives its own life
    under the control of the container in which it's stored. Storing a copy
    could require more memory and be generally slower than storing a
    pointer... I am sure there are other differences, and you can probably
    find lots of interesting stuff about those in any decent book on C++.
    What book are you currently reading?

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 22, 2012
    #2
    1. Advertising

  3. Ruby Stevenson <> wrote:
    > So one is store a pointer to event; the other store a copy of the
    > event in a container.
    > what are the implications between the two choices? when would be the
    > proper use of one instead of the other?


    Storing the Event objects by value is much safer (and depending on
    its size, often more efficient) than storing pointers to dynamically
    allocated Event objects.

    However, storing them by value requires that Event objects can be
    copied (which means that if Even contains any dynamically allocated
    resources, those have to be handled properly in its copy constructor
    and assignment operator). If Event contains only basic (non-pointer)
    types and class objects (which themselves have working copy constructors)
    as members then you don't need to do anything special about it, and it
    will work just fine. However, as said, if Event contains dynamically
    allocated resources (in most cases having a pointer as member is usually
    an indication of this).

    Also take into account that if Event is very large (eg. it could contain
    things like large std::vectors as members) copying will be a heavy
    operation.

    Storing dynamically allocated Event objects is error-prone because it
    requires you to delete them appropriately (or else you'll get a memory
    leak). Usually, if this is required, it's better to use a smart pointer
    instead of a raw pointer.

    Storing them as pointers allows runtime polymorphism (which storing by
    value does not allow). In other words, you could store in the vector
    objects of type Event or any object derived from it. This is about the
    only advantage of storing pointers instead of objects. (The other is,
    as said, if the objects are very large and are heavy to copy.)
     
    Juha Nieminen, Feb 22, 2012
    #3
  4. Ruby Stevenson

    Christopher Guest

    On Feb 22, 6:37 am, Ruby Stevenson <> wrote:
    > All -
    >
    > I have a newbie question. Let me try to use an example to illustrate:
    > I have some Event class defined, and I could save a collection of
    > event objects in two ways:
    >
    > method 1:
    >     vector<Event*> event_vec;
    >     ....
    >
    >     Event * event = new Event();
    >     event_vec.push_back(event);
    >
    > method 2:
    >    vector<Event> event_vec;
    >    ....
    >    Event event;
    >    event_vec.push_back(event)
    >
    > So one is store a pointer to event; the other store a copy of the
    > event in a container.
    > what are the implications between the two choices? when would be the
    > proper use of one instead of the other?
    >
    > Thanks
    >
    > Ruby


    A little ranting on the side, since the main question has been
    answered. It's a pet peave of mine to use such generic names as
    "event". Try to get into the habit of naming the class for what it
    really is and the variable for what it really is. Unless of course,
    you are designing it to be a truly generic event.

    Example: KeyPressed keyPress;
     
    Christopher, Feb 22, 2012
    #4
  5. Ruby Stevenson

    Jorgen Grahn Guest

    Naming (was Re: clarification on pointer use)

    On Wed, 2012-02-22, Christopher wrote:
    > On Feb 22, 6:37 am, Ruby Stevenson <> wrote:
    >> All -
    >>
    >> I have a newbie question. Let me try to use an example to illustrate:
    >> I have some Event class defined, and I could save a collection of
    >> event objects in two ways:
    >>
    >> method 1:
    >>     vector<Event*> event_vec;
    >>     ....

    ....

    > A little ranting on the side, since the main question has been
    > answered. It's a pet peave of mine to use such generic names as
    > "event". Try to get into the habit of naming the class for what it
    > really is and the variable for what it really is. Unless of course,
    > you are designing it to be a truly generic event.
    >
    > Example: KeyPressed keyPress;


    Wouldn't that be obvious from context in a real program, though? For
    all we know, Event may be local to keypress.cc, to class Keyboard, or
    in some appropriate namespace.

    It's a pet peeve of /mine/ when people duplicate context information;
    for example I find it easier to read keyboard.event than
    keyboard.keyboardEvent (or keyboard.keyboardEvent.keyCode).

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Feb 22, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ma740988
    Replies:
    5
    Views:
    393
    ma740988
    Aug 23, 2004
  2. Replies:
    3
    Views:
    381
    Victor Bazarov
    Feb 2, 2005
  3. Makwana

    Pointer clarification.

    Makwana, Aug 26, 2008, in forum: C++
    Replies:
    2
    Views:
    318
    peter koch
    Aug 26, 2008
  4. Peter

    pointer/linked List clarification

    Peter, Dec 23, 2008, in forum: C Programming
    Replies:
    5
    Views:
    673
    Peter
    Dec 26, 2008
  5. Tricky

    Pointer clarification needed

    Tricky, Sep 9, 2009, in forum: VHDL
    Replies:
    3
    Views:
    549
    Tricky
    Sep 9, 2009
Loading...

Share This Page