This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Bug or another C++ oddity?
- From: George Garvey <tmwg-gcc at inxservices dot com>
- To: gcc at gcc dot gnu dot org
- Date: Sun, 25 Nov 2001 21:40:57 -0800
- Subject: Bug or another C++ oddity?
- Organization: inX Services, Los Angeles, CA, USA
I expected if a const member function were called, that the const
functions of a member attribute would be called. This is not the case,
as shown below.
Is this just another of my many misunderstandings of what to expected
from the language?
#include <iostream>
using namespace std;
struct S {
inline S() {;}
inline int i() const {
return 1;
}
inline int i() {
return 0;
}
};
struct T {
S *s;
inline int i() const {
return s->i();
}
inline int j() const {
return ((const S *)s)->i();
}
inline T() {s = new S;}
inline ~T() {delete s;}
};
int
main() {
const S sc;
cout << sc.i() << endl;
S ss;
cout << ss.i() << endl;
const T tc;
cout << tc.i() << endl;
T ts;
cout << ts.i() << endl;
cout << tc.j() << endl;
cout << ts.j() << endl;
return 0;
}
output:
1
0
0
0
1
1
I expected that T::i() would always call the const i() in S, because
T::i() is a const function. I expected that meant that T::s would be
const, too.
I can work around it with the T::j construct, but I'd like to know if
its a bug or not.
If its a bug, I'll file a PR. Otherwise, I may get flamed for asking
in the wrong place.