Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!att!cbnews!cbnewsm!gregk From: gregk@cbnewsm.att.com (gregory.p.kochanski) Newsgroups: comp.lang.c++ Subject: Re: calling main in ANSI C Summary: more on comst. Which function gets picked? Keywords: More on const. Message-ID: <1990Dec4.055744.2191@cbnewsm.att.com> Date: 4 Dec 90 05:57:44 GMT References: <814@atcmpe.atcmp.nl> <535@taumet.com> Distribution: usa Organization: AT&T Bell Laboratories Lines: 44 Should functions be selected based on the type of their result? Generally, no. How about on the 'constness' of their result? Imagine we have a class with const and non-const members. I'm thinking particularly of an array class with reference counting: class array { pointer_to_class_with_data_and_reference_count ..... const int& operator[](int index) const; int& operator[](int index); }; Now in a situation like this, the two functions have drastically different costs to execute. The const version merely returns a reference to some spot in memory -- fast and cheap. On the other hand the non-const version has to copy the entire data array, on the assumption that an element is about to be modified. So far, so good. Now, suppose you're going to use this class, and you have a function that makes some modifications to the array: void x(array& q) { q[0] = q[1]; ... q[2] = q[3]; ... } Now, on half of these calls, we're just 'reading' from the array, but we're still paying the heavy overhead for the non-const functions. Admittedly, they could just as well be written as q[0] = ((const array&)q)[1], but in practice, that's a pain in the neck. So, one could propose that if both 'const' and 'non-const' versions of a function could be applied, and if the *result* could be const, then the 'const' version would be selected. This would make some code much more elegant. It's a real problem that I've run into several times. Is it worth the added complexity? Greg Kochanski gpk@physics.att.com Brought to you by Super Global Mega Corp .com