Re: I don't know what's wrong with my very simple code

From: Karl Heinz Buchegger (kbuchegg_at_gascad.at)
Date: 01/08/04


Date: Thu, 08 Jan 2004 17:55:48 +0100

Sam wrote:
>
> > >class point
> > >{
> > >public:
> > > void init(pose P);
> >
> > I think you intended to pass by reference:
> >
> > void init(pose& P);
> >
> I just want to make it clearer. Here the fn init(pose P) is a friend
> of pose, so it can modify P's private values. It does not have to use
> reference to modify P's private values.

Right.
But that init function is called from somewhere. Eg.

int main()
{
  point A;
  pose B;

  A.init( B );

Well. As you have written it now, a copy of B is made and given to init().
init() now modifes this copy. But this also means that B is never touched
and modified. In some situations this is indeed what someone wants to
achieve, but most of the time it is not. More often you want the function
to change the object given to it. In this case you have to use a reference.

Also: make it a habit of yours to pass class objects by reference.
If you want the function to able to change the callers object, you
use an ordinary reference:

   void point::init( pose& Arg )
   {
      Arg.i = 2;
   }

If you don't want the function to be able to change the callers argument,
you pass by const reference:

   void point::init( const pose& Arg )
   {
      Arg.i = 2;
   }

Now the compiler will emit an error, saying that i of Arg cannot be changed,
since you promised that it is const - non changable.

The reason why you want to pass a reference is, that when you pass by value

   void point::init( pose Arg )

a copy of the callers pose object is made. Depending on the object itself this
can be a (very) costly operation. Well in your case the pose object seems to
be simple enough that this would not be a problem, but there is more to it:
The pass by reference doesn't cost you much more then creating a copy of the
object, so it doesn't really matter which one you choose (in this example). Therefore
you are in the situation: in the worst case, both passing mechanisms are cost equal
but in the best case the pass by reference saves you from a costly operation. So use
pass by reference and be on the safe side at all times.

The only exception is: If you definitly don't want to change the callers argument,
but yet have to work with that object and change it locally in that function, you can
use a pass by value. But even then it is arguable if one should pass per const reference
and create the copy explicitely to document the feature that you know what you are
doing and you know that the change is not reflected to the caller of that function:

   void point::init( const pose& Arg )
   {
     pose ArgCopy( Arg ); // creating a copy of what the caller passed

     ArgCopy.i = 3; // I know that the assignment takes place at a
                             // copy of whatever the caller has passed to this
                             // function and thus the assignment will not be reflected
                             // to outside this function
   }

-- 
Karl Heinz Buchegger
kbuchegg@gascad.at


Relevant Pages

  • Re: Iraq
    ... >seems to answer the remaining questions you pose. ... This seems to be a reference to HUAC, Mr Smith, not McCarthy. ...
    (comp.lang.cobol)
  • Re: OT -- No guns involved
    ... The proof is in google. ... what proof do we have that you'd pay attention ... it shouldn't pose a problem to him to give a reference to this ...
    (rec.scuba)
  • Re: stupid question...waiting for a stupid answer
    ... private void notifyChanges{ ... Unless you believe that a boolean can be passed by reference, ... void called(int x, Holder y, Holder z) ...
    (comp.lang.java.programmer)
  • Re: [PHP] Can a class instance a property of another class
    ... Do they just serve to pass the object by reference? ... private $dbconn; ... $conn = new DBConn; ... $a = new DOExample; ...
    (php.general)
  • some config error happens periodically only
    ... An error occurred during the processing of a configuration file ... Policy not being applied to reference at this time (private, custom, ... Attempting download of new URL ...
    (microsoft.public.dotnet.framework.aspnet)