Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!att!dptg!ulysses!andante!alice!ark From: ark@alice.UUCP (Andrew Koenig) Newsgroups: comp.lang.c++ Subject: Re: warning message from Sun C++ 2.0 Message-ID: <10264@alice.UUCP> Date: 20 Dec 89 00:26:05 GMT References: <1286@sunquest.UUCP> Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 49 In article <1286@sunquest.UUCP>, francis@sunquest.UUCP (Francis Sullivan) writes: > class Str { > public: > Str(Str&); > Str(char *); > Str(); > ~Str(); > Str& operator= (Str& s1); > Str friend operator+ (Str& s1, Str& s2); > }; > The program works fine, but I want to be able to do operations like above > without getting the warning message. Any solutions besides delaring temporary > strings for " " and the strings built from argv? People who use references as arguments usually do so for one of two reasons: (1) they want to be able to modify the argument. (2) they want to avoid copying the argument for efficiency and do not intend to modify it. If you're using a reference for reason (1), it's bad news for you to pass a temporary, because anything you change will be discarded with the temporary. Thus the warning, which future releases will make into an error. If your motivation is (2), you should announce your intention not to modify the argument by using a reference to a constant: class Str { public: Str(const Str&); Str(const char *); Str(); ~Str(); Str& operator= (const Str& s1); Str friend operator+ (const Str& s1, const Str& s2); }; Note also the use of `const char*' rather than `char*' -- you probably don't intend to modify the argument of the Str constructor. -- --Andrew Koenig ark@europa.att.com