473,404 Members | 2,170 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,404 software developers and data experts.

returning references and pointers (to C++ objects)

I'm in the process of implementing an abstract factory design pattern
for an application. I would like to know, which is the recommended way
of returning objects from the factory - by reference or by pointer?

Returning pointers is a no brainer, but I'm not sure how to return a
reference to a newly created object, in a method call. (Yes, I know
about RAII).

Pseudocode:

DerivedClass& AbstractFactory::foo( const std::string& s, ...) {
...
BaseClass *p = new DerivedClass( ) ;
BaseClass &ref = p ;
return p ;
}
Is this correct ?

Thanks

Jul 23 '05 #1
3 4284
Alfonso Morra wrote:

I'm in the process of implementing an abstract factory design pattern
for an application. I would like to know, which is the recommended way
of returning objects from the factory - by reference or by pointer?

Returning pointers is a no brainer, but I'm not sure how to return a
reference to a newly created object, in a method call. (Yes, I know
about RAII).

Pseudocode:

DerivedClass& AbstractFactory::foo( const std::string& s, ...) {
...
BaseClass *p = new DerivedClass( ) ;
BaseClass &ref = p ;
return p ;
}

Is this correct ?


No. A reference is another name for an *object*, not for a pointer.
Thus you need the object, the pointer points to. You get to the
object by dereferencing the pointer, thus

BaseClass &ref = *p;

(The other problems in your code above will be sorted out by the compiler).

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #2
Alfonso Morra wrote:
I'm in the process of implementing an abstract factory design pattern
for an application. I would like to know, which is the recommended way
of returning objects from the factory - by reference or by pointer?
By pointer. If you are dealing with dynamic memory, use pointers. Users
of your factory may forget to delete the pointers, that's their
problem, but they *will* forget to delete the address of the
references, and that's because of you.

When I get a reference, dynamic memory does not even come to my mind.
Returning pointers is a no brainer, but I'm not sure how to return a
reference to a newly created object, in a method call. (Yes, I know
about RAII).

Pseudocode:

DerivedClass& AbstractFactory::foo( const std::string& s, ...) {
...
BaseClass *p = new DerivedClass( ) ;
BaseClass &ref = p ;
return p ;
}
No:

C &f()
{
C *p = new C;
return *p;
}

Is this correct ?


No, neither the code nor the reasoning. Return a pointer when you deal
with dynamic memory.
Jonathan

Jul 23 '05 #3


Jonathan Mcdougall wrote:
Alfonso Morra wrote:
I'm in the process of implementing an abstract factory design pattern
for an application. I would like to know, which is the recommended way
of returning objects from the factory - by reference or by pointer?

By pointer. If you are dealing with dynamic memory, use pointers. Users
of your factory may forget to delete the pointers, that's their
problem, but they *will* forget to delete the address of the
references, and that's because of you.

When I get a reference, dynamic memory does not even come to my mind.

Returning pointers is a no brainer, but I'm not sure how to return a
reference to a newly created object, in a method call. (Yes, I know
about RAII).

Pseudocode:

DerivedClass& AbstractFactory::foo( const std::string& s, ...) {
...
BaseClass *p = new DerivedClass( ) ;
BaseClass &ref = p ;
return p ;
}

No:

C &f()
{
C *p = new C;
return *p;
}

Is this correct ?

No, neither the code nor the reasoning. Return a pointer when you deal
with dynamic memory.

Typo, I meant to return ref, instead of the pointer.

Jonathan


Jul 23 '05 #4

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

Similar topics

0
by: {RainmakeR} | last post by:
Hi all, I've noticed something strange with returning references, both in PHP4 and 5 - maybe it's supposed to do this, I don't know - but it seems a bit weird nonetheless. Say we have the...
2
by: Phil... | last post by:
I am trying to figure out how to create an array that contains objects and not references to objects. I know how to do the latter, but not the former. Any help is appreciated. I have the...
1
by: Karlo Basic | last post by:
Hi! I'm still just learning to program in C++ and I've got a question about returning references. Below is the code of my program. I think it's pretty obvious what it does... What doesn't work...
18
by: cppaddict | last post by:
Hi, Is it considered bad form to have the subscript operator return a const reference variable? If not, what is the proper way to do it? My question was prompted by the code below, my...
10
by: Jax | last post by:
I dont seem to fully comprehend references to objects yet. Lets say for example I do this; Customer c = new Customer(); Customer c1 = c; I understand that if I change c1, I also change c as...
9
by: nano2 | last post by:
Hi , Have the following case passing in structure pointers and returning structure pointers can anyone spot whats wrong with this structure name is structCar void callfn( ){
8
by: howa | last post by:
from PHP manual, it said: Do not use return-by-reference to increase performance, the engine is smart enough to optimize this on its own ------------------ Why?
23
by: pauldepstein | last post by:
Below is posted from a link for Stanford students in computer science. QUOTE BEGINS HERE Because of the risk of misuse, some experts recommend never returning a reference from a function or...
6
by: laredotornado | last post by:
Hi, I'm using PHP 5. I have $this->m_pages_arr = array_merge($this->m_pages_arr, $p_file_obj- in which "$this->m_pages_arr" is an array and $p_file_obj is an object I defined. What I...
2
by: BobLewiston | last post by:
The concept of delegates (references to methods) per se is new to me (although I used function pointers in C years ago), and although the literature acknowledges that there are significant...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...
0
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,...
0
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...

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.