[KDev4] let's clearify VCS interfaces
Kris Wong
wongk at seapine.com
Mon Apr 2 12:40:47 UTC 2007
>> QMap& KSomeClass::getMap()
>> {
>> QMap map;
>> map.insert(xxxx);
>> return map;
>> }
>> This is what I meant. The returned reference is invalid because it
>> is destroyed after the function returns.
>>
>> Below is correct.
>> void KSomeClass::getMap( QMap &map )
>> {
>> map.insert(xxx);
>> }
>
> Well, we can make it QMap& KSomeClass::getMap() with this:
>
> return QMap(map);
>
> Because then the returned reference won't be destroyed but still
contain
> a copy of the map. You know I don't like parameters like QMap& too
much,
> unless they're really needed. It can easily be confused with const
QMap&
> and people will wonder what the parameter is supposed to do...
Why return a reference here? Qt container classes only do shallow
copies, and returning a reference is technically not correct in the case
of building a temporary.
QMap KSomeClass::getMap()
{
QMap map;
map.insert(xxxx);
return map;
}
Kris Wong
More information about the KDevelop-devel
mailing list