Path: utzoo!utgpu!watserv1!watmath!att!hriso!attdso!westmark!mole-end!mat From: mat@mole-end.UUCP (Mark A Terribile) Newsgroups: comp.lang.c++ Subject: Re: const Summary: overload on the const Message-ID: <436@mole-end.UUCP> Date: 8 Sep 90 04:50:50 GMT References: <10863@cadillac.CAD.MCC.COM> Organization: mole-end--private system. admin: mole-end!newtnews Lines: 44 In article <10863@cadillac.CAD.MCC.COM>, vaughan@puma.cad.mcc.com (Paul Vaughan) writes: . . . > So, here's a const question. . . . > Consider the following function . . . > const char* substring(const char* string, const char* sub) > // substring searches for the first occurence of sub in string and > //returns a pointer to it or 0 if none is found. . . . > [But what if I want to operate on non-const characters??] Yup, you've got a real problem, though not an insurmountable one. Starting with Release 2.0, you should be able to overload on const/non-const-ness . You can't do it on the basis of the return type, but you can do it on the basis of the first string. const char* substring( const char* string, const char* sub ); char* substring( char* string, const char* sub ); Does this mean that you have to implement it twice (ignoring that the first substring() may already exist elsewheres)? Weeeeeelllll ... I don't think it unreasonable to write inline char* substring( char* string, const char* sub ) { return (char*) substring( (const char*) string, sub ); } There are some places where this sort of thing won't work; you really need different algorithms. It would be nice to be able to say that a function's return type is const if certain actual arguments are const. This is probably not something we will see soon, except perhaps in templates. Unfortunately, when you use templates, it is expected (last time I checked) that a weaker form of argument matching will be applied than for ordinary functions. -- (This man's opinions are his own.) From mole-end Mark Terribile