473,387 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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.

Aug 29 '08 #1
3 1154
On 29 Aug, 10:54, "Alf P. Steinbach" <al...@start.nowrote:
* 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
Aug 29 '08 #2
On Aug 29, 2:09 pm, Triple-DES <DenPlettf...@gmail.comwrote:
On 29 Aug, 10:54, "Alf P. Steinbach" <al...@start.nowrote:
* 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:ja*********@gmail.com
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
Aug 31 '08 #3
In article <f656b761-4573-4c5f-842b-4b41466536c0
@k36g2000pri.googlegroups.com>, ac******@gmail.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<Somethingresults;

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.
Sep 1 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Danimal | last post by:
I have been using PHP for a long time... since it was called PHP/FI. I have a programming design question: Let say I have this class: class attrib { var $lenght; var $type; ... }
9
by: Patchwork | last post by:
Hi Everyone, I have a design related question (in C++) that I am hoping someone can help me with. It is related to my previous post but since it was pointed out that I was more or less asking...
3
by: Amadelle | last post by:
Hi All and thanks in advance, I wanted to know when is a good idea to use a static class (with static constructor) and when to use instance classes? I have read couple of articles on line and...
13
by: KV | last post by:
I'm new to OO Design, and I'm fixing to start writing my very first C# program. Given the complexity of OO programming, I would like to run something by this group and get general input. My...
17
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
10
by: Sjaakie | last post by:
Hi, I'm, what it turns out to be, fooling around with 3-tier design. At several websites people get really enthusiastic about using custom dataobjects instead of datasets/-tables. While trying to...
0
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that...
0
weaknessforcats
by: weaknessforcats | last post by:
Design Patterns: Visitor Introduction Polymorphism requires a class hierarchy where the interface to the hierarchy is in the base class. Virtual functions allow derived classes to override base...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.