Design question: where to create objects as result of a function

Discussion in 'C++' started by Alex, Aug 29, 2008.

  1. Alex

    Alex Guest

    Hi everyone,

    I have a question that implies thinking about some design issues with C
    ++.

    I have a method like this:

    class C {
    ....
    public:
    void get_something(std::vector<Something*>& results);
    }

    It returns a vector of objects that should be created (or filled in
    with data) by the class C.

    The problem is, if the Something objects are created inside
    get_something, I will have to delete them later outside the function,
    and I must avoid that for several reasons.

    I don't know how many objects will be in the vector, so I can't create
    the objects in advance and pass a vector of references to be filled in
    by the function.

    What is your advice for this kind of situations?

    Thanks.
     
    Alex, Aug 29, 2008
    #1
    1. Advertising

  2. Alex

    Alex Guest

    Hi, thanks for the answer. Wouldn't it very inefficient to return by
    value a whole vector?

    And, also, I need to use references/pointers to Something, not
    variables in the stack...

    On 29 ago, 01:26, "Alf P. Steinbach" <> wrote:
    > * Alex:
    >
    >
    >
    > > Hi everyone,

    >
    > > I have a question that implies thinking about some design issues with C
    > > ++.

    >
    > > I have a method like this:

    >
    > > class C {
    > > ...
    > > public:
    > >    void get_something(std::vector<Something*>& results);
    > > }

    >
    > > It returns a vector of objects that should be created (or filled in
    > > with data) by the class C.

    >
    > > The problem is, if the Something objects are created inside
    > > get_something, I will have to delete them later outside the function,
    > > and I must avoid that for several reasons.

    >
    > > I don't know how many objects will be in the vector, so I can't create
    > > the objects in advance and pass a vector of references to be filled in
    > > by the function.

    >
    > > What is your advice for this kind of situations?

    >
    > > Thanks.

    >
    >    std::vector<Something> something() const;
    >
    > Cheers & hth.,
    >
    > - Alf
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?
     
    Alex, Aug 29, 2008
    #2
    1. Advertising

  3. Alex

    Triple-DES Guest

    On 29 Aug, 10:54, "Alf P. Steinbach" <> wrote:
    > * Alex:
    > > And, also, I need to use references/pointers to Something, not
    > > variables in the stack...

    >
    > Why.
    >


    Wild guess: His class is a base class with virtual functions. Or does
    not have value semantics.

    In which case the "politically correct" advice is to return an
    std::vector< boost::shared_ptr<T> >

    DP
     
    Triple-DES, Aug 29, 2008
    #3
  4. Alex

    James Kanze Guest

    On Aug 29, 2:09 pm, Triple-DES <> wrote:
    > On 29 Aug, 10:54, "Alf P. Steinbach" <> wrote:


    > > * Alex:
    > > > And, also, I need to use references/pointers to Something, not
    > > > variables in the stack...


    > > Why.


    > Wild guess: His class is a base class with virtual functions.
    > Or does not have value semantics.


    > In which case the "politically correct" advice is to return an
    > std::vector< boost::shared_ptr<T> >


    Which is usually bad advice. But without knowing the role of
    the objects in his vector, we can only speculate. The most
    likely "correct" solution is what Alf proposed: don't use
    pointers at all. And just return the vector. Just about
    anything else counts as a special case, which requires its own
    special rules.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Aug 31, 2008
    #4
  5. Alex

    Jerry Coffin Guest

    In article <f656b761-4573-4c5f-842b-4b41466536c0
    @k36g2000pri.googlegroups.com>, says...
    > Hi everyone,
    >
    > I have a question that implies thinking about some design issues with C
    > ++.
    >
    > I have a method like this:
    >
    > class C {
    > ...
    > public:
    > void get_something(std::vector<Something*>& results);
    > }
    >
    > It returns a vector of objects that should be created (or filled in
    > with data) by the class C.
    >
    > The problem is, if the Something objects are created inside
    > get_something, I will have to delete them later outside the function,
    > and I must avoid that for several reasons.
    >
    > I don't know how many objects will be in the vector, so I can't create
    > the objects in advance and pass a vector of references to be filled in
    > by the function.
    >
    > What is your advice for this kind of situations?


    Have get_something take an iterator instead of a vector. Call it
    something like:

    std::vector<Something> results;

    C c;
    c.get_something(std::back_inserter(results));

    Personally, I'd look at C (especially C::get_something) with an eye to
    whether you can reasonably turn it into a functor to (for example)
    produce the results one at a time, so you'd use it with a standard
    algorithm.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Sep 1, 2008
    #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. 7stud
    Replies:
    11
    Views:
    710
    Dennis Lee Bieber
    Mar 20, 2007
  2. J.Ram
    Replies:
    7
    Views:
    660
  3. Pavel
    Replies:
    7
    Views:
    546
    Pavel
    Sep 19, 2010
  4. Lakshmi Sreekanth

    i = 10; result = ++i - --i; How result become ZERO

    Lakshmi Sreekanth, Sep 21, 2010, in forum: C Programming
    Replies:
    52
    Views:
    1,197
    Nick Keighley
    Sep 23, 2010
  5. Michael Tan
    Replies:
    32
    Views:
    1,021
    Ara.T.Howard
    Jul 21, 2005
Loading...

Share This Page