Path: utzoo!attcan!uunet!cimshop!davidm From: cimshop!davidm@uunet.UU.NET (David S. Masterson) Newsgroups: comp.lang.c++ Subject: Re: const Message-ID: Date: 7 Sep 90 01:12:21 GMT References: <10863@cadillac.CAD.MCC.COM> <142@tdatirv.UUCP> Sender: davidm@cimshop.UUCP Distribution: comp Organization: Consilium Inc., Mountain View, California. Lines: 41 In-reply-to: sarima@tdatirv.UUCP's message of 5 Sep 90 15:59:11 GMT In article <142@tdatirv.UUCP> sarima@tdatirv.UUCP (Stanley Friesen) writes: But I believe you *can* overload it on the type of the first argument based on the presence/absence of 'const'. In other words, declare it *both* the first and the second way. This will then call either the const or non-const version depending on whether the first argument is a const string or not. The point is that: const char* substring(const char*, ...) // first argument is important for this discussion has a very unuseful return value (because its a constant). Changing it to: char* substring(char*, ...) makes the return value useful again, but removes the hint about what might be optimized by the compiler (like whether its valid to place the arguments or returns in registers). Note also that for the substring() function: char* substring(const char*, ...) is not possible (in this argument) because of the definition that "once a const, always a const" (the return value will be a pointer to something in the argument, therefore, its a const char*). Because of that definition, overloading these last two is not possible. The point is that in this example, you would like to use CONST on the argument in order to make it plain that the function will not be changing the argument (which provides optimization information for the function). However, you don't want to use CONST on the return value because it is likely that the user of the function will want to change the substring once substring() found it. But, by definition of "once a const, always a const", this does not seem to be possible. -- ==================================================================== David Masterson Consilium, Inc. uunet!cimshop!davidm Mtn. View, CA 94043 ==================================================================== "If someone thinks they know what I said, then I didn't say it!"