By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,128 Members | 1,206 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,128 IT Pros & Developers. It's quick & easy.

returning references and pointers (to C++ objects)

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a


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 discussion thread is closed

Replies have been disabled for this discussion.