473,657 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(s td::vector<Some thing*>& 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 1170
On 29 Aug, 10:54, "Alf P. Steinbach" <al...@start.no 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 "politicall y correct" advice is to return an
std::vector< boost::shared_p tr<T

DP
Aug 29 '08 #2
On Aug 29, 2:09 pm, Triple-DES <DenPlettf...@g mail.comwrote:
On 29 Aug, 10:54, "Alf P. Steinbach" <al...@start.no 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 "politicall y correct" advice is to return an
std::vector< boost::shared_p tr<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 objektorientier ter Datenverarbeitu ng
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.go oglegroups.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(s td::vector<Some thing*>& 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<Som ethingresults;

C c;
c.get_something (std::back_inse rter(results));

Personally, I'd look at C (especially C::get_somethin g) 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
1739
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
2459
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 the wrong questions about the wrong 'topic' (polymorphism) I have posted this new question. Please don't see this as a spurious attempt to repost :-) As mentioned previously, there is a very real problem I am trying to solve, but I have reduced...
3
1419
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 the general example is when you want to write a log file use static class ...but they don't say why? My specific question is that I have a validation class with some data validation methods which will be called through out the program and I am...
13
2092
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 example is a program called HijackThis. I'm sure many are familiar that it is a spyware removal tool. The program looks at over 20 places on Windows computers to see what is starting (with options to remove the offending software). The program can...
17
2694
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 know nothing about ASP. He can build the design of the pages in HTML with tables, labels, textboxes etc. But then I would need to change them to ASP.net objects and write the code to make the page work (normally I do this as I go - can't do this...
15
1898
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 two? (1)
10
2771
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 write such layers myself I got stuck on how to get filtered or sorted data from the data-layer. This is what I got: Objects
0
2502
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 don't work nearly as well as they should, even for analysts and power users. The reason they haven't reached the masses is because most of the tools are so difficult to use and reveal so little
0
11613
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 class functions. Applications using polymorphism typically have functions with base class pointers or references as arguments. Then derived objects are created and used as arguments to these functions. Inside the function, only the base class methods...
0
8403
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8316
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8509
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7345
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6174
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5636
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1730
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.