开发者

Is a "const member function" checked at compile time or runtime?

开发者 https://www.devze.com 2023-03-06 20:10 出处:网络
I am wondering when the checking for a const member function happens? I guess it happens 开发者_如何转开发at compile time, but cannot be sure.

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.


constness 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 constness 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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号