Path: utzoo!attcan!uunet!mcsun!ub4b!kulcs!herman From: herman@kulcs.uucp (Herman Moons) Newsgroups: comp.lang.c++ Subject: const member functions: why bother the programmer? Message-ID: <2026@kulcs.kulcs.uucp> Date: 23 Jul 90 15:45:24 GMT Organization: Kath.Univ.Leuven, Comp. Sc., Belgium Lines: 43 Why does the C++ language demand that the programmer states which member functions are const ?? This seems a job for the compiler, not for the programmer (after all, the compiler checks whether a function is really const if we specify it that way). I guess the const keyword is needed to tell the compiler about the const'ness of a member function when it is invoked for a const class object (the member function is perhaps implemented in another file, where the compiler cannot see it). But why not encode the const-info into the function name, just like the parameter type info ?? A member function f that is const (which can be determined by the compiler) could than be given two entries in the symbol table, one with and one without the const-info. e.g. f T 0xaaaaa --- entry 1 C_f T 0xaaaaa --- entry 2 When a non-const object calls f(), entry 1 is looked up. When a const object calls f(), a call for C_f is made. When the member function was not const, the C_f info would not have been generated, and a link error would occur. Without the const keyword, errors would also be catched (at link time, and not at compile time). So why bother the programmer with these technicalities. What really bothers me about const member functions is that one must provide the compiler with information that he already knows. Any opinions on this topic ?? herman ----------------------------------------------------------------------------- Herman Moons Katholieke Universiteit Leuven Dept. of Computer Science Tf : +32 (16) 20.06.56 Celestijnenlaan 200A Fax: +32 (16) 20.53.08 B-3030 Heverlee-Leuven Belgium e-mail: herman@kulcs.uucp herman@blekul60.bitnet herman@cs.kuleuven.ac.be -----------------------------------------------------------------------------