Is a "const member function" checked at compile time or runtime?
I am wondering when the checking for a const
member function happens? I guess it happens 开发者_如何转开发at compile time, but cannot be sure.
Does anyone know?
It happens at compile time. In C++ almost all the type checking is done at compile time. The only exception to this is when you use dynamic_cast
.
So your const
member function is checked at compile time.
A const
member function is checked at compile time.
const
ness of member functions affects the type of the this
pointer. Type safety is checked at compile time (unless bypassed with a cast, most casts bypass safety checks, dynamic_cast
adds a runtime check).
However, on most architectures const
ness is not merely a type check, but enforced by the memory protection unit. This happens at runtime, and is NOT bypassed by const_cast
(or C-style cast).
精彩评论