Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > is it safe to create an object inside a function and pass out in std::vector?

Reply
Thread Tools

is it safe to create an object inside a function and pass out in std::vector?

 
 
zl2k
Guest
Posts: n/a
 
      11-15-2010
hi, there,

Is it safe if I do like this

void PopulateVector(std::vector<Object> objs){
Object obj;
objs.push_back(obj);
}

Will the obj created inside of the function lost once I try to use it
outside of the function? I need the function to populate an empty
vector. Thanks for your comments.

zl2k
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      11-15-2010
On 11/16/10 11:45 AM, zl2k wrote:
> hi, there,
>
> Is it safe if I do like this
>
> void PopulateVector(std::vector<Object> objs){
> Object obj;
> objs.push_back(obj);
> }
>
> Will the obj created inside of the function lost once I try to use it
> outside of the function? I need the function to populate an empty
> vector. Thanks for your comments.


Pass by reference:

void PopulateVector( std::vector<Object>& objs )

Objects are copied into containers, so yes it will be OK.

--
Ian Collins
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      11-16-2010
zl2k <> wrote:
> hi, there,
>
> Is it safe if I do like this
>
> void PopulateVector(std::vector<Object> objs){


I assume you mean:

void PopulateVector(std::vector<Object>& objs){

> Object obj;
> objs.push_back(obj);
> }
>
> Will the obj created inside of the function lost once I try to use it
> outside of the function? I need the function to populate an empty
> vector. Thanks for your comments.


Yes, the object created inside the function will be "lost" when the
function is exited. That's what local objects do, by design. However, you
are not putting the object in question in the vector. You are telling the
vector to *copy* the object in question (in other words, the vector will
create a *new* object in its innards, and copy-construct it with the
object you gave it as parameter). As long as the object has a
properly-working copy constructor (which might be the compiler-generated
one), it will work ok.
 
Reply With Quote
 
Ruslan Mullakhmetov
Guest
Posts: n/a
 
      11-16-2010
On 11/16/2010 1:52 AM, Ian Collins wrote:
> On 11/16/10 11:45 AM, zl2k wrote:
>> hi, there,
>>
>> Is it safe if I do like this
>>
>> void PopulateVector(std::vector<Object> objs){
>> Object obj;
>> objs.push_back(obj);
>> }
>>
>> Will the obj created inside of the function lost once I try to use it
>> outside of the function? I need the function to populate an empty
>> vector. Thanks for your comments.

>
> Pass by reference:
>
> void PopulateVector( std::vector<Object>& objs )
>
> Objects are copied into containers, so yes it will be OK.
>


You could also use pointer instead of reference if you know that param
is going to be changed.
As for me i do not use non-const references at all except operators
overloading. Using pointers you definitely know that object could be
changed or specify 0 (NULL) if you do not want return value.

by the way, i've read about such rule in google code style guide.
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      11-17-2010
On Tue, 2010-11-16, Ruslan Mullakhmetov wrote:
> On 11/16/2010 1:52 AM, Ian Collins wrote:

....
>> void PopulateVector( std::vector<Object>& objs )

....
> You could also use pointer instead of reference if you know that param
> is going to be changed.
> As for me i do not use non-const references at all except operators
> overloading. Using pointers you definitely know that object could be
> changed


In other words:

- pass const Foo& when you don't modify the argument
- pass Foo* to signal that you do
- don't pass Foo&

Some people use that rule yes, but it's far from universal.
One reason is:

> or specify 0 (NULL) if you do not want return value.


You probably don't want to let the user pass NULL, most of the time.

> by the way, i've read about such rule in google code style guide.


If I recall correctly, that guide has been criticized a lot here.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      11-18-2010
On Nov 16, 11:51*pm, Ruslan Mullakhmetov <tiaba...@gmail.com> wrote:
> On 11/16/2010 1:52 AM, Ian Collins wrote:
>
>
>
>
>
> > On 11/16/10 11:45 AM, zl2k wrote:
> >> hi, there,

>
> >> Is it safe if I do like this

>
> >> void PopulateVector(std::vector<Object> objs){
> >> Object obj;
> >> objs.push_back(obj);
> >> }

>
> >> Will the obj created inside of the function lost once I try to use it
> >> outside of the function? I need the function to populate an empty
> >> vector. Thanks for your comments.

>
> > Pass by reference:

>
> > void PopulateVector( std::vector<Object>& objs )

>
> > Objects are copied into containers, so yes it will be OK.

>
> You could also use pointer instead of reference if you know that param
> is going to be changed.
> As for me i do not use non-const references at all except operators
> overloading. Using pointers you definitely know that object could be
> changed or specify 0 (NULL) if you do not want return value.


Passing NULL as output parameter when caller does not want that output
is the way of C interface. In C++ it is rude when interface demands
parameters that caller does not have; in C++ you can have overloads.

> by the way, i've read about such rule in google code style guide.


Some other style guides suggest to minimize amount of raw pointers and
avoid usage of unary &. I like such rules better.
 
Reply With Quote
 
Miles Bader
Guest
Posts: n/a
 
      11-19-2010
Öö Tiib <> writes:
> Some other style guides suggest to minimize amount of raw pointers and
> avoid usage of unary &. I like such rules better.


Yeah, me too.

There's obviously a tradeoff, and neither way is "perfect"; it would be
cool if c++ had a way to indicate "out/modified" parameters, just to
make source-code more readable while preserving the goodness of
references over pointers.

Of course simply by use of references to const vs. non-const objects,
one can indicate this in the function interface to some degree, but I
gather what proponents of using pointers for out-arguments want is a way
of making it obvious at the _call-site_ that an argument is an output.

E.g.

...
SomeType output;
...
some_func (inp1, inp2 + 3, out: output);

substitute your prefered notation for "out:", e.g., "=>" might also be
fairly readable:

some_func (inp1, inp2 + 3, => output);

Since const-vs-non-const largely covers the actual type-checking, I
guess this would be largely cosmetic... I suppose to make sure people
actually remembered to use such notation, declarations could also have
these annotations, and enforce the rule that "if the declaration marks a
parameter as "out:" (or "=>"), then calls must also do so."

-Miles

--
You can hack anything you want, with TECO and DDT.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to pass C++ object to another C++ function via Python function grbgooglefan Python 9 05-09-2008 01:34 PM
Defining a function inside a function. Whats this feature ? How touse inside a class ? Sur Ruby 4 01-08-2008 02:50 PM
how to pass a function name and its arguments inside the arguments of other function? jmborr Python 1 11-03-2007 08:20 AM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
how to dynamically create a function object (from a code object)? Petri Savolainen Python 0 07-01-2003 10:43 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57