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
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