returning references and pointers (to C++ objects)

A

Alfonso Morra

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
 
K

Karl Heinz Buchegger

Alfonso said:
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).
 
J

Jonathan Mcdougall

Alfonso said:
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
 
A

Alfonso Morra

Jonathan said:
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.




No:

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




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.
 

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,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top