Game Development Community

Any point in foo(const U32) ?

by Orion Elenzil · in Technical Issues · 03/28/2006 (6:02 pm) · 3 replies

Hello programmers.
this one is just out of curiosity.

there's a few methods in Torque which take const U32 as a parameter.
is there any point in making that const ?

for example:

virtual void lineInserted(const U32);

cheers,
orion


edit: removed a small bit of torque-specific code.

#1
03/28/2006 (7:47 pm)
I think in this case its mostly about good form; "const correctness" is the basis for less buggy code. But one point would be that not even the implementation of lineInserted() could modify the arg's value on the stack. Since it is not a reference (pointer) it stops there. There's some tangential issues about overriding the method in the base class and unique method signatures,etc., but those seem a little pointless in this case.

Most books will say that declaring things in their most safe form is a best practice and one should only relax the style when it is necessary and intended.

Just my two cents.
#2
03/28/2006 (8:00 pm)
Hi Tim,

It's certainly better & more consistent form, i agree, tending to better 'self documentation'.

and should you ever replace that "U32" with like "myAmazingStruct&", the const will be there, ready to enforce consthood!

on the flip side,
i think it clutters up the code and makes it harder to read.


.. const-hood is enforced by the linker, not the runtime, right ?

hmm. i wonder if this could cause slightly more overhead in exotic polymorphic situations. probably minimal.
#3
03/28/2006 (8:07 pm)
Should be enforced in two ways: compile time and linking. The compiler should notice any descrepancy and I think the linker will catch any polymorphic changes to any override method signatures.

LOL, I got curious and tested this by modifying void GuiMessageVectorCtrl::lineInserted( const U32 arg) by removing the const. VC7 didn't catch it! So maybe my settings aren't strict. Hmmm..