"J. Campbell" <ma**********@yahoo.com> wrote in message
news:b9**************************@posting.google.c om...
Suppose I have an object that is concatinatable (sp) using the '+'
operator (eg string). I have a function(func)that will do some work
to the object and return an int. For several reasons it is desirable
to have the ability to call the function like this:
int myvalue = func(obj1 + obj2);
because of this am I limited to passing by value? If I demand a pass
by reference, must my calling procedure look like this,
AnObj temp = obj1 + obj2;
intmyvalue = func(temp);
rather than concatinating inside the function parentheses? This second
method defeats the purpose of pass-by-reference, since it creates a
new object anyway.
I realize that I could make the function declaration look like
int func(AnObj&, AnObj&);
but this is undesirable since the function really only needs one
AnObj.
Any advice?
Joe
Well, I'm not sure exactly what your goal is so I'm not sure I can offer
advice that will help, but I'll offer a couple of observations...
You noted the creation of a second object in this case:
AnObj temp = obj1 + obj2;
intmyvalue = func(temp);
Well, there is a second object created in this case too:
int myvalue = func(obj1 + obj2);
In this case, the object is an unseen temorary, but it is real nonetheless.
For this reason, you could pass only by *const* reference. A non-const
reference may not be bound to a temporary, only a const reference may be.
And of course, if your goal is to have the called function modify its
parameter, you're SOL. If, however, you're not wanting to pass by reference
to have the called function modify its parameter but rather for efficiencies
sake, you could then change the parameter to a const reference and make the
call func(obj1 + obj2);.
So, I'm not sure if this will help but I hope it does...