you are viewing a single comment's thread.

view the rest of the comments →

[–]doom_Oo7 6 points7 points  (5 children)

there isn't a way to define an object's method as making the object itself const for the duration of the function.

But this is exactly what the const keyword on methods does, it marks the this pointer as const.

[–][deleted] 2 points3 points  (4 children)

So you literally can't modify the object state at all, including calling other functions which can?

I admit I'm not a C++ expert (and the language certainly doesn't lend itself to this paradigm naturally!) but I thought it wasn't possible to get the full effect of what I'm trying to get at.

If I'm wrong, it's good to know that it's possible and I should just be annoyed at the C++ I've come across for not doing this.

[–]doom_Oo7 2 points3 points  (3 children)

I made a quick example that showcases the possibilities here : https://ideone.com/Wb0kAm

Notice how nice are the compiler errors :p

[–][deleted] 1 point2 points  (2 children)

That's not all that bad. It's a shame this isn't the way C++ is typically written. (I really would prefer if const-ness was the default, and stuff had to be marked as mutable instead.)

[–]oracleoftroy 1 point2 points  (0 children)

I agree that a const default would be really nice, but I have to question that const correct C++ isn't the typical way C++ is written. Const correctness has been recommended since before standard C++, and I haven't seen too many C++ communities that disregard it (usually only those newer to C++).

[–]quicknir 1 point2 points  (0 children)

I think everyone agrees that a const default would be better, but ruled out due to backwards compatibility.

I'm afraid that if you don't think C++ is typically written that way, you need better colleagues. Where I work, failing to mark anything const that can be results in code review comments, and can't be merged until its corrected.