Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!uunet!microsoft!jimad From: jimad@microsoft.UUCP (Jim Adcock) Newsgroups: comp.lang.c++ Subject: Re: warning message from Sun C++ 2.0 Message-ID: <10010@microsoft.UUCP> Date: 20 Dec 89 18:06:29 GMT References: <1286@sunquest.UUCP> Reply-To: jimad@microsoft.UUCP (Jim Adcock) Organization: Microsoft Corp., Redmond WA Lines: 31 In article <1286@sunquest.UUCP> francis@sunquest.UUCP (Francis Sullivan) writes: >I have just started learning C++, and was experimenting with a string class for >practice. Using g++ 1.34.2, I don't get warning messages with the following >program, but I do with Sun C++ 2.0. [...] >class Str { > public: > Str(Str&); > Str(char *); > Str(); > ~Str(); > Str& operator= (Str& s1); > Str friend operator+ (Str& s1, Str& s2); >}; ... I added the keyword "const" to all explicet parameters of all member functions of Str and the warnings went away. Without the "const" keyword, the compiler assumes the member functions could modify the referred-to objects, and may introduce temporaries to protect against this presumed modification. Declare parameters "const" if they will not be modified by the routine using them. Declare member functions "const" if they do not modify "this." Not only is this an important part of documenting a routine, it can also lead to significantly better generated code.