RE: const and call by value parameters

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi,



"Mark" wrote:

> I have a function
>
> void myobject::g (double d)
> {
> dval = d;
> }
>
> Now function g() doesn't modify it's local copy of d in any way, therefore
> it should be declared as
>
> void myobject::g (const double d) {}

Actually, you omit const in this case. I use const in case when a funstion
MUST NOT modify its parameter, rather than when it DOESN'T modify
its parameter. Consider the following code;

double dVal = 5;
obj.g(dVal); // your function
// We are sure that dVal is 5 (hasn't been modified), since
// g(double d) doesn't take its parameter as a reference -
// g(double & d);
double dSomeOtherVal = dVal; // 5

So, if g(double d) doesn't disturb the outer stuff, it is its problem
to handle 'd' properly, and since g's body is very simple and you may
see that 'd' doesn't carry mutation before being used, you may simply omit
'const' in the parameter.

Use const with references (or pointers) if the parameter is not supposed
to be changed:

void myobject::g(const double &d);

Practically, such parameters are objects of some classes
that do not have copy constructor, or aren't small enough
to be copied instantly.


> The thing is, I find that the vast majority of my call by value parameters
> aren't reused within their functions, therefore I find that I have an awful
> lot of function parameters using the const modifier.
>
> When I look at other code, in books or in libraries, you don't tend to see
> const littered throughout the code, even in functions that don't modify
> their value parameters.
>
> Am I missing something here?
>
> --
> Best regards
> Mark
>
>



--
======
Arman
.



Relevant Pages

  • const and call by value parameters
    ... Now function gdoesn't modify it's local copy of d in any way, ... void myobject::g ... lot of function parameters using the const modifier. ... Mark ...
    (microsoft.public.vc.language)
  • Code critique please
    ... struct SortOrder1: SortOrder ... bool operator()(const A &lhs, const A &rhs) ... class Mark ... void Tree::Insert ...
    (comp.lang.cpp)
  • Re: Making C# properties act like member variables
    ... > return const objects so that I wouldn't have to worry about the client ... > holds a reference to the FloatRgba version of diffuseColor and can modify ... it returns a new FloatRbga and change FloatRbga so that it is immutable. ... so you have to think about modifiability when designing the type not ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Using const qualifier
    ... But it returns a pointer through which you could modify the list. ... Do you use "const" to imply "I will not change this, ... const int *operate; ... constraint: typeofis typeof; ...
    (comp.lang.c)
  • Re: const and call by value parameters
    ... >> void f ... > I agree with you, but if you declare those parameters with const, you can ... > I use const as Mark do, and it's true that it could make more difficult ... but I think it's good to avoid code like showed above. ...
    (microsoft.public.vc.language)