Topic: overload resolution based on const-ness of member functions: where?
Author: leavens@cs.iastate.edu (Gary Leavens)
Date: 22 Feb 95 14:20:52 GMT Raw View
It seems that C++ compilers (e.g., g++) allow one to overload
member functions based on the presence or absence of a const-qualifier
for the member function. I looked in vain in the 20 September 1994 standard
draft for a statement as to why this should be so, or whether it should
be prohibited, but can't find anything. If it's not there, perhaps the
committee should consider saying something about it. If it is there,
please let me know where...
The issue is illustrated by the the following program, in 3 files.
//////////////////////////////////////////////////////////
// Foo.h
class Foo {
public:
Foo();
int bar(int i) const;
int bar(int i);
private:
int x;
};
//////////////////////////////////////////////////////////
// Foo.C
#include <iostream.h>
#include "foo.h"
Foo::Foo()
{
x = 0;
}
int Foo::bar(int i) const
{
cout << "const member function called" << endl;
return 1;
}
int Foo::bar(int i)
{
cout << "non-const function called" << endl;
return 0;
}
//////////////////////////////////////////////
// FooClient.C
#include "foo.h"
int main()
{
Foo myFoo;
const Foo aFoo;
myFoo.bar(3);
aFoo.bar(4);
return 0;
}
/////////////////////////////////////////////////
This program prints
% ./fooClient.exe
non-const function called
const member function called
Which means that it calls the const member function on const objects,
and the non-const one on non-const objects.
To summarize, where does the draft standard say this is ok and what
should happen?
Gary Leavens
--
229 Atanasoff Hall, Department of Computer Science
Iowa State Univ., Ames, Iowa 50011-1040 USA / leavens@cs.iastate.edu
phone: (515)294-1580 fax: (515)294-0258 ftp site: ftp.cs.iastate.edu
URL: http://www.cs.iastate.edu/~leavens/homepage.html