Go Back   Rhinocerus > Newsgroup > Newsgroup comp.language.c++

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 02-22-2012, 11:37 AM
Ruby Stevenson
Guest
 
Posts: n/a
Default clarification on pointer use

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
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 02-22-2012, 02:00 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: clarification on pointer use

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
Reply With Quote
  #3 (permalink)  
Old 02-22-2012, 02:48 PM
Juha Nieminen
Guest
 
Posts: n/a
Default Re: clarification on pointer use

Ruby Stevenson <ruby185@gmail.com> 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.)
Reply With Quote
  #4 (permalink)  
Old 02-22-2012, 07:52 PM
Christopher
Guest
 
Posts: n/a
Default Re: clarification on pointer use

On Feb 22, 6:37*am, Ruby Stevenson <ruby...@gmail.com> 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;
Reply With Quote
  #5 (permalink)  
Old 02-22-2012, 09:34 PM
Jorgen Grahn
Guest
 
Posts: n/a
Default Naming (was Re: clarification on pointer use)

On Wed, 2012-02-22, Christopher wrote:
> On Feb 22, 6:37*am, Ruby Stevenson <ruby...@gmail.com> 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 .
Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 06:29 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.