Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a method that waits for a change of state be const?

Tags:

c++

constants

In a multithreaded scenario, I have a method like this:

bool WaitForChange( time_duration WaitTime ) const;

This method waits either until the state of the object has changed and returns true, or until the timeout times out (how do you say that?) and returns false.

My intuition is, that const is to protect against unwanted side-effects of the method itself, so this is fine. But then again, some user might think that the state of the could not have changed, since the method is declared const. Is that user stupid, or should I make the method non-const in order to avoid confusion?

like image 1000
Björn Pollex Avatar asked Mar 26 '10 14:03

Björn Pollex


2 Answers

By declaring the method as const, you say "Calling this method doesn't change the state of the object." This is (hopefully) true. So make it const.

If anybody thinks, const-ness means "While this method is called, no one else can change the object state" than that person is wrong.

like image 175
Sebastian Avatar answered Nov 05 '22 05:11

Sebastian


I vote for constness.

The method itself does not change anything, just waits...

like image 33
Viesturs Avatar answered Nov 05 '22 07:11

Viesturs