This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Bug or another C++ oddity?


   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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]