We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

const function arguments: style question

mrwad99
mrwad99 asked
on
Medium Priority
283 Views
Last Modified: 2010-04-01
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
Comment
Watch Question

Commented:
Mrwad, this sounds more like a question of style to me. You could leave in const to allow other programmers to know
that it will not be changed throughout the code. Also, if you leave it const, it will make sure you later don't start messing
with it. It may be important that the value not change due to usage throughout the function. Perhaps you expect it to
be the initial value throughout, if someone else modified it, they may ruin calculations. For this reason, const is good,
perhaps if someone sees const, they will review the code before making any changes which could ruin it.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
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.
>>>> void Function ( const CMyClass i )     // overkill ?

I personally don't think it is a question of style but it is bad programming. If you want to make it const use a const reference. That is much more efficient than passing it by value where a copy constructor and a new storage is needed.

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.

>>>> perhaps if someone sees const, they will review the code before making any changes which could ruin it.

If that argument is valid you should pass a const reference and all is perfect. The only exception I would make is with smart pointers where the *reference* is made within the copy constructor. Here, you may pass a const value if you don't want tthe smart pointer be used for non-const operations (though it is still better to pass a const reference of a smart pointer).

Regards, Alex

Commented:
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.

Commented:
>>>>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

Commented:
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

           
}

Author

Commented:
Thanks both :)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.