Xref: utzoo comp.lang.c++:6130 gnu.g++:594 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uunet!nih-csl!keith From: keith@nih-csl.UUCP (keith gorlen) Newsgroups: comp.lang.c++,gnu.g++ Subject: Re: function modifiers Summary: I think this must be a g++ bug Message-ID: <1310@nih-csl.UUCP> Date: 14 Jan 90 04:48:22 GMT References: <14940@robin.cs.nott.ac.uk> Organization: NIH-CSL, Bethesda, MD Lines: 39 In article <14940@robin.cs.nott.ac.uk>, gas@cs.nott.ac.uk (Alan Shepherd) writes: > > In the code for the NIH library, there are several declarations of the > following form: > > const Object* Object::method() const; > > and also: > > friend const Object* Object::method() const; > > I've tried to compile this with g++-1.36.2 and discovered that friends > aren't allowed to have function modifiers in g++ i.e the terminating const. In C++ 2.0, you can overload member functions based on their const-ness; i.e., you can declare a const member function that has the same name and arguments as a non-const member function. This is very useful. Consider: class String { // ... char& operator[](int); const char& operator[](int) const; }; // ... const String s = "A constant string"; String t = "A variable string"; t[1] = 'x'; // OK -- t[1] calls String::operator[](int), which returns a char& char z = s[1]; // OK -- s[1] calls String::operator[](int) const, which returns a const char& s[1] = 'x'; // Error -- can't assign to a const If you want to make a const member function a friend, you must be able to specify the const modifier in the friend declaration to uniquely identify it, since it may be overloaded in this fashion. -- Keith Gorlen phone: (301) 496-1111 Building 12A, Room 2033 uucp: uunet!nih-csl!keith National Institutes of Health Internet: keith@alw.nih.gov Bethesda, MD 20892