[gnu.g++] function modifiers

gas@cs.nott.ac.uk (Alan Shepherd) (01/09/90)

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. 
Looking at the gnu manual, it seems that the use of the terminating const
(see 6.12) supposedly has a meaning unique to g++, yet must be legal syntax
for AT&T 2.0 since the library is written for that compiler.  

Removing the terminating const causes lots of other related error messages
to do with const member functions calling non-const member functions etc.
Unfortunately, we don't have AT&T C++ or a reference manual, so I'm unsure
of the precise effect the use of const.

Could someone clarify this for me please and perhaps clear up the differences
between AT&T and G++ ?

Alan Shepherd

comeau@utoday.UUCP (Greg Comeau) (01/11/90)

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 ...:
>	  const Object* Object::method() const;
>          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. 
>Removing the terminating const causes lots of other related error messages
>to do with const member functions calling non-const member functions etc.
>
>Alan Shepherd

This is correct.  The thing is a const member function can be used with
both const data and non-const data, however a non-const member function can
only be used with non-const data so removing the const means that any
const object that was being manipulated by the previous const function
can no longer be manipulated (where manipulated is really == modified)
since it must maintain the semantics of operations performed on const
data whether that data be member data or non-member data (say an external
global const instance of a class).

So since NIH is working code though, removing the const means that
when a const MF calls a non-const MF it has no way to keep track of what
the non-const function is doing.  In cfront 2.0 this produces a warning
though and not an error.
-- 
Greg, Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418
Also, mag writer for UNIX Today! (SysAdm columnist), Microsoft Systems Journal
(C programming), + others. Also, BIX c.language & c.plus.plus conf. moderator.
Here:attmail!csanta!greg / BIX:comeau / CIS:72331, 3421 / voice:718-849-2355

keith@nih-csl.UUCP (keith gorlen) (01/14/90)

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