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

A

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.
 
A

Alex

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...
 
T

Triple-DES

* Alex:

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
 
J

James Kanze

[/QUOTE]
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.
 
J

Jerry Coffin

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top