Re: Refactoring Patch
"Kristian G. Kvilekval" <[email protected]>
| Newsgroups | gmane.comp.audio.zinf.devel |
|---|---|
| Message-ID | <1060801716.32504.8.camel@merrimack> |
On Wed, 2003-08-13 at 10:47, Ralf Engels wrote:
> However I disagree in one point:
>
> > For class like objects, references always make sense:
> >
> > 1. read-write types should be "type&" as an argument
> > 2. read-only types should be "const type& " as an argument
> >
> > This stems from the fact that passing large objects on
> > the stack by value can be quite costly in terms of runtime
> > performance.
>
> If you see a function call like this
>
> doIt( p1 );
>
> You will think that p1 is not modified. Call by value is the standard
> case with C.
>
> However someone could define doIt like this:
>
> doIt( ObjType* &p1)
>
> and now I will spend half an hour looking where the heck p1 is initialized.
>
> In Java the same problem does not appear. There p1 could be modified if
> it is an Object but never set!
>
> So I prefere to define doIt like this:
>
> doIt( ObjType* *p1 )
>
> In this case the call to doIt( &p1 ) makes it absolutly clear that p1
> can be set.
A pointer type is a builtin type, the rules above mention passing
around class types. Still, the best in all cases is to return
the value from the function, when that is not possible (multiple return
values, r/w variable, return is used for error codes), then either a
reference or a pointer is ok for built-in types.
Again when I see type& , I know the object may be modified,
when I see const type&, know the object will never be modified.
I would also like to method calls labeled const. For example
class C {
int get_a_field () const;
}
The const let's me(and the compiler) know that the calling
the method will never modify the target of the method.
> At last I want to say that C++ has good and bad points. A small call
> with std::string can lead to a lot of mallocs because the object could
> be copied around.
> Good point is that you can call by reference and use const to state that
> the function will not modify the reference.
Most/all string classes do internal reference counting and use
copy-on-write semantics for dealing with copies. They are
quite efficient after the initial hit of loading all the code.
> More patches will follow...
>
> BR,
> Ralf
>
>
>
>
>
>
> -------------------------------------------------------
> This SF.Net email sponsored by: Free pre-built ASP.NET sites including
> Data Reports, E-commerce, Portals, and Forums are available now.
> Download today and enter to win an XBOX or Visual Studio .NET.
> http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01
> _______________________________________________
> Zinf-devel mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/zinf-devel
--
email:[email protected] office:(805)893-4276 http://www.cs.ucsb.edu/~kris
-------------------------------------------------------
This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data Reports, E-commerce, Portals, and Forums are available now.
Download today and enter to win an XBOX or Visual Studio .NET.
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01