[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-pim
Subject:    Re: [Kde-pim] kabc question
From:       Cornelius Schumacher <schumacher () kde ! org>
Date:       2002-05-29 10:47:19
[Download RAW message or body]

On Wednesday 29 May 2002 00:05, Reinhold Kainhofer wrote:
>
> If you use something like
>
> void setEmail(KABC::Addressee a, QString mail) {
> a->insertEmail(mail); }
>
> void doSync(...) {....
> 	setEmail(myAddressee, "test@domain.tld");
> 	setEmail( myAddressee, "test2@another.domain.tld");
> }
>
> doesn't the by-value call to setEmail create a copy of the
> myAddressee?

Yes, if you pass the Addressee by value the setEmail() function will 
operate on a copy. That means it will not have the effect you desire, 
because the email address will be added in the copy which will be 
deleted when the function returns. It has no effect. That's exactly 
what you should expect as it's the same with every other type of 
object.

Copying an addressee is, by the way, a very cheap operation because the 
data is not copied but only the pointer to the data. That means until 
you change the data in the addressee, the data is shared. That's 
implicit sharing (sometime also called "copy-on-write").

> (You see, my theoretical C++ knowledge is not so
> profound, it's just what I remember from reading Stroustrup some long
> time ago). If so, does a definition like
>
> void setEmail(KABC::Addressee &a, QString mail) {...}
>
> prevent this, since the Addressee is passed by reference? If so, with
> your patch one really needs to take care to use the &a in all
> methods!

Not more than with every other object. If you want the function to 
change the object in the calling code you need a reference or a 
pointer. If you just want to pass the value to be used inside the 
function a "const Addressee &a" argument is the most appropriate.

> I still think your patch is the correct and clean solution to the
> problem, we just need to make sure developers understand that they
> need to pass by reference if they don't want to do some unnecessary
> stuff.

I don't think that Addressee needs the developer to know something 
special. The only thing is that the class is implicityly shared which 
means that it's perfectly ok to pass the class by value without loosing 
performance.

-- 
Cornelius Schumacher <schumacher@kde.org>
_______________________________________________
kde-pim mailing list
kde-pim@mail.kde.org
http://mail.kde.org/mailman/listinfo/kde-pim
kde-pim home page at http://pim.kde.org/
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic