Link to home
Start Free TrialLog in
Avatar of mrwad99
mrwad99Flag for United Kingdom of Great Britain and Northern Ireland

asked on

const function arguments: style question

Ah hello.

If I have a function like this:

void Function ( CMyClass& i )

and I intend to not modify the parameter i, it is a good idea to change the function to

void Function ( const CMyClass& i )

However, what if I am passing the parameter by value - is the const still necessary ?  I mean, it only prevents against modifying the local copy of the parameter:

void Function ( const CMyClass i )      // overkill ?

Is it also good form to use const in this case, or just overkill ?

TIA
SOLUTION
Avatar of List244
List244

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mrwad99

ASKER

Yeah; it is a style question.  You have raised a good point about possibly preventing other people from modifying the code if they see that a variable is const, that I would not have thought of myself.

I will leave this open a little longer to see if there is any more feedback.

Thanks.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of List244
List244

Well, I agree, it would be better to use a reference. Though, you should note, there is a difference between using const int& and
const int. If you use a const int, and a coder modifies the value using a const cast, the value will NOT be affected outside of the
function. If you do the same with const int&, the value WILL be changed when leaving the function. Setting it to const does not
make it unmodifiable.
>>>>If you pass an argument by const value the called function needs one more copy to get a writeable copy what seems to me some kind of dictation that simply is bad.

Itsmeandnobodyelse I am confused by what you are saying here?
>>>>  I am confused by what you are saying here?

I meant that defining a const argument is a restriction that makes no sense. If the one who defines the interface is the same who is implementing the function it is some kind of self restriction. If there is more than one person involved - e. g. in case of virtual functions - the restriction not needed and less efficiency of the design is bad. Or take the case where the object passed is a baseclass object. When passing it by value all information of the derived class where the object was copied from was lost.  

Generally, a good design shouldn't make any restrictions - and const is a severe restriction - which is not needed or can be made more effciently.

Regards, Alex
But if you are already passing byvalue, what is the harm in stating that it is const? If it is meant to be const,
it is a good warning to anyone that modifies the code. I agree that byref would be better, but as far as const
byvalue and non-const byvalue, is there a difference in performance? In other words, would it cause any
additional harm to change the function:

void Func(int Val)
to
void Func(const int Val)
>>>> But if you are already passing by value

... you can make it non-const

No harm but not not needed,

>>>> void Func(const int Val)

That prevents from using 'Val' as a non-const. Again, no harm but a unnecessary restriction.

If for example you have

int rfind(const char* str, const char c, const int len)
{
     while (len > 0)
          if (str[--len] == c)   // error
                 return len;
     return -1;
}    

you'll get an error. For class types an additional copy may not generate an exact copy but generate a new (internal) id what isn't that what you want.

Regards, Alex

           
}
Avatar of mrwad99

ASKER

Thanks both :)