[Bug c++/30331] a const member function can call a non_const member function without const_cast
hongleij at 126 dot com
gcc-bugzilla@gcc.gnu.org
Sat Dec 30 13:49:00 GMT 2006
------- Comment #4 from hongleij at 126 dot com 2006-12-30 13:48 -------
(In reply to comment #3)
> the question is : if a const member function can call a non_const member
> function
> without const_cast,
> if fun() changed the member in A,like this:
> //const_test.cpp
> struct A
> {
> A(unsigned int n)
> {
> aa=n;
> }
> void const_fun() const
> {
> static_cast<A>(*this).fun(); //ok
> // fun(); //err
> }
> void fun()
> {
> aa=78;
> }
> unsigned int aa;
> };
>
> int main()
> {
> const A a(56);
> a.const_fun();
> }
> the sematic of bitwise constness is not confirmed.
> this program is inspired from the book <Effective C++>(third edition)
> Item3: use const whenever possible
i do hope the compiler at least give a worning!
>
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30331
More information about the Gcc-bugs
mailing list