Re: Questing regarding returning of new objects
On Sep 9, 1:23 pm, Erik Wikstr=F6m <Erik-wikst...@telia.com> wrote:
On 2007-09-09 12:58, thorsten.schill...@gmail.com wrote:
the question is, what is the "golden way" or the best way,
if I have a memberfunction in a class, which should return a
new instance of an object. For example some class Foo which
holds a lot data and has the overloaded '+' operator and I
want to do something like:
//f1-f3 are all Foo instances
f1 = f2+f3;
Is it in that case better to work with pointers in general like:
Foo* Foo::operator+(Foo& f){
//Do stuff
return new Foo([lot,of,data]);
}
No, the + operator should return a new object to keep the
semantics sane.
100% agreed. The semantics of the final program must always be
as if operator+ returned a new object. And not a new'ed object;
it's almost impossible to use an operator+ which returns a
new'ed object without leaking memory.
Of course, the the returned object doesn't have to be of type
Foo. A frequent solution is to return a proxy of some sort.
to avoid the copying of the data which is hold by the new
object at the end of the function? Or is there any sensefull
way to implement that with a reference (Foo&
Foo::operator+(Foo& f)), since it is more intuitive? Just
imagine that the class Foo is somekind of Matrixclass with a
big 2 dimensional array as member variable. Or is the only
way just to write Foo that it holds only a pointer to that
big variable? Sure, there are numerous solutions, but I
would like to know which is the most common and most elegant
by some experienced C++ developers.
There are two ways to solve this, the best but hardest to
implement is to use expression templates, unless you are
writing a library that will be used in many different
applications this is probably too much work.
It's actually not that much work, total. It's a lot of extra
code, but it's grunt code, which doesn't require much thought,
once the design has been decided on. (I've not tried, since I
haven't needed such in my applications, but I'll bet you could
write a simple script to generate it: you specify the base type
and the desired operators, and the script generates the rest.)
Note too that you don't really need templates for this;
inheritance works just as well. The technique was being used
long before C++ compilers supported templates. (It's a curious
case of inheritance, since the virtual functions are all inline,
and all of the objects of the derived class are normally
temporaries, generated by the compiler on the stack, and whose
dynamic type is known to the compiler, so it doesn't have to
generate the usual code for a virtual function call.)
The other solution is to not use the + operator and instead
use the += operator:
Foo f1(f2);
f1 += f3;
That puts the onus on the user, and is a real pain for more
complicated expressions (which require explicit temporaries).
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34