473,241 Members | 1,504 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,241 software developers and data experts.

Is it best to return a class or a reference to a class?

Tom
If I have a container class that has a map member which stores pointers
to objects that have been created via the new operator and I have a
method that returns a entry in the map, would it be best to return the
entry as a dereferenced pointer so it becomes a class or return a
reference to the dereferenced class?

I believe that it would be best to dereference the class pointer
retrieved from the map and then send it back as a reference to the
caller. This would ensure that any manipulations to the returned class
would be reflected in the class stored in the container objects map?

It may be that the container class will be shunted around the
application and the data contained in it manipulated in various methods
and functions.

TIA,
Tom.

Sep 12 '06 #1
5 2034
Tom wrote:
If I have a container class that has a map member which stores pointers
to objects that have been created via the new operator and I have a
method that returns a entry in the map, would it be best to return the
entry as a dereferenced pointer so it becomes a class or return a
reference to the dereferenced class?

I believe that it would be best to dereference the class pointer
retrieved from the map and then send it back as a reference to the
caller. This would ensure that any manipulations to the returned class
would be reflected in the class stored in the container objects map?

It may be that the container class will be shunted around the
application and the data contained in it manipulated in various methods
and functions.

TIA,
Tom.
If you want to manipulate the returned data with the manipulation
taking effect on the original data object, you need to return either
references or pointers.

Sep 12 '06 #2
Tom wrote:
If I have a container class that has a map member which stores
pointers to objects that have been created via the new operator and I
have a method that returns a entry in the map, would it be best to
return the entry as a dereferenced pointer so it becomes a class or
return a reference to the dereferenced class?

I believe that it would be best to dereference the class pointer
retrieved from the map and then send it back as a reference to the
caller. This would ensure that any manipulations to the returned
class would be reflected in the class stored in the container objects
map?

It may be that the container class will be shunted around the
application and the data contained in it manipulated in various
methods and functions.
Something doesn't add up. If you have a requirement to allow the
code that receives the [object or reference] to change the object
actually stored, then how can you return another object? Unless your
objects are wrappers (proxies), you copy the data from your storage,
the recipient changes the copy, nothing is changed in your storage.
I don't see that you have any choice.

Now, if you don't want to allow the recipient of your stored data
to manipulate it, you have a choice of either returning an object or
a reference to const. You need to decide whether it's worth copying
the object (if not, return a reference to const) or whether the other
party can try to hold onto the returned value which can change at
your own discretion. If it's important that the object reflected
the changes you make to it even if the third party holds onto the
returned "value", return a reference - the recipient will always have
the fresh version (although they might need to be notified that the
changes have actually occurred). If you specifically don't want to
let the changes propagate (the recipient gets a "snapshot" of the
value and not a "handle" to it), then return an object.

It seems that you haven't supplied enough information to answer your
question, or you just haven't thought of all possibilities yourself.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 12 '06 #3
Tom
Victor Bazarov wrote:
Tom wrote:
If I have a container class that has a map member which stores
pointers to objects that have been created via the new operator and I
have a method that returns a entry in the map, would it be best to
return the entry as a dereferenced pointer so it becomes a class or
return a reference to the dereferenced class?

I believe that it would be best to dereference the class pointer
retrieved from the map and then send it back as a reference to the
caller. This would ensure that any manipulations to the returned
class would be reflected in the class stored in the container objects
map?

It may be that the container class will be shunted around the
application and the data contained in it manipulated in various
methods and functions.

Something doesn't add up. If you have a requirement to allow the
code that receives the [object or reference] to change the object
actually stored, then how can you return another object? Unless your
objects are wrappers (proxies), you copy the data from your storage,
the recipient changes the copy, nothing is changed in your storage.
I don't see that you have any choice.

Now, if you don't want to allow the recipient of your stored data
to manipulate it, you have a choice of either returning an object or
a reference to const. You need to decide whether it's worth copying
the object (if not, return a reference to const) or whether the other
party can try to hold onto the returned value which can change at
your own discretion. If it's important that the object reflected
the changes you make to it even if the third party holds onto the
returned "value", return a reference - the recipient will always have
the fresh version (although they might need to be notified that the
changes have actually occurred). If you specifically don't want to
let the changes propagate (the recipient gets a "snapshot" of the
value and not a "handle" to it), then return an object.

It seems that you haven't supplied enough information to answer your
question, or you just haven't thought of all possibilities yourself.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
The scenario is this

The original values to the map are created in the initial function of
the application. Further processing in various functions throughout
the application need to use the data in the map and sometimes actually
change the data held in the map.

Rather than have to extract the original objects in the map and replace
them when they are modified, it would be nice to simply retrieve a
modifiable version of the objects that reflect the changes in the
original without the need to have to delete the original and add the
new version or even replace the original with the newer version.

I think JFK has already provided the answer I had a feeling would be
the preferred which is to store/retrieve the pointer to the original
object so that any changes are indeed reflected in the objects stored
in the map.

Thanks,
Tom

Sep 12 '06 #4
Tom wrote:
The scenario is this

The original values to the map are created in the initial function of
the application. Further processing in various functions throughout
the application need to use the data in the map and sometimes actually
change the data held in the map.

Rather than have to extract the original objects in the map and
replace them when they are modified, it would be nice to simply
retrieve a modifiable version of the objects that reflect the changes
in the original without the need to have to delete the original and
add the new version or even replace the original with the newer
version.
Or, perhaps you should give the object that owns the map to make the
changes necessary... Was it yesterday that I read about Law of Demeter
here? Or was it in a different newsgroup?... I don't remember. Check
it out, though.
I think JFK has already provided the answer I had a feeling would be
the preferred [...]
I am glad that his/her guess was right. Next time do give more thought
to the design and provide more information.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 12 '06 #5
Tom

Victor Bazarov wrote:
Tom wrote:
The scenario is this

The original values to the map are created in the initial function of
the application. Further processing in various functions throughout
the application need to use the data in the map and sometimes actually
change the data held in the map.

Rather than have to extract the original objects in the map and
replace them when they are modified, it would be nice to simply
retrieve a modifiable version of the objects that reflect the changes
in the original without the need to have to delete the original and
add the new version or even replace the original with the newer
version.

Or, perhaps you should give the object that owns the map to make the
changes necessary... Was it yesterday that I read about Law of Demeter
here? Or was it in a different newsgroup?... I don't remember. Check
it out, though.
<snip>

Although I agree with you that the Law of Demeter is correct, I think
you are missing the point here. LoD does not preclude the use of
databases/repositories or transport mechanisms and/or does not
interpret the database/repository or transport mechanism as the
"friend" or as a stranger.

The class that holds the map in this scenario is simply a container for
application data. The only purpose of it is to transport application
data around the application. Think of it similar to a shopping cart,
though that is not the use here but a good analogy, in that the
container holds various types of application data types in the same way
that a shopping cart can hold different inventory.

In this way it is both a friend and neutral, in that it is presents a
known interface to the application in order for the application to
store data in a retrieve data from using setter/getter methods. Other
than that, the application knows nothing about it.

The data classes contained in the class map are known application data
types which the application manipulates. Given that there is a need to
manipulate the data in various different points in the application and
and unknown quantity of the data, it serves well to store/retrieve
references/pointers to the data classes such that they can be
manipulated without the overhead of copy ctors or assignment operators.
The alternative would be to hold global arrays/maps/vectors/lists of
the data types which is not very nice to deal with.

Simply put I have created a container specific to the needs of my
application and as such it should be compared to using a std::map or
std::vector or any other std::container for that matter. LoD does not
preclude the use of such containers.

Thanks,
Tom.

Sep 13 '06 #6

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

Similar topics

0
by: Sasha | last post by:
Hi everybody, I would like to hear your thoughts on the following problem. We have the following classes. Class Exam int ID* int Version* string Name
3
by: Sasha | last post by:
Hi everybody, I would like to hear your thoughts on the following problem. We have the following classes. Class Exam int ID* int Version* string Name
4
by: Chuck Ritzke | last post by:
I keep asking myself this question as I write class modules. What's the best/smartest/most efficient way to send a large object back and forth to a class module? For example, say I have a data...
3
by: RubiconXing | last post by:
Hi All Beginner question - please be patient with me :-) I am new to c#. If one creates a child modal (and also non-modal) form what is the best way of returning the collected data back to the...
15
by: cppaddict | last post by:
I have class with two static member objects, one of type int and one of type vector<int>. static int myStaticMemberInt static vector<int> myStaticMemberVector; I know how to initialize the...
5
by: Robert Zurer | last post by:
I have a large pool of business objects all referencing one another in various ways. In the client application I want to do something like employee.Delete(); Behind the scenes, I want to...
14
by: Jon Rea | last post by:
I am currently cleaning up an application which was origainlly hashed together with speed of coding in mind and therefore contains quite a few "hacky" shortcuts. As part of this "revamping"...
13
by: andrea | last post by:
Sorry for the stupid question, I know, but sometimes is necessary starts from the basic. I don't know how to pass the result of a method generated from a DAL class to a BL class returning the...
2
by: Anthony Biondo Jr | last post by:
I am trying to figure out the best way to return data through a web service. If the value is a single value I can just set it equal to the web service name. If I am returning a set of data I have...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.