Solved

const function arguments: style question

Posted on 2006-10-23
9
231 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
0
Comment
Question by:mrwad99
  • 4
  • 3
  • 2
9 Comments
 
LVL 8

Assisted Solution

by:List244
List244 earned 35 total points
ID: 17788770
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.
0
 
LVL 19

Author Comment

by:mrwad99
ID: 17789274
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.
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 40 total points
ID: 17789872
>>>> 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

0
 
LVL 8

Expert Comment

by:List244
ID: 17789959
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.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 8

Expert Comment

by:List244
ID: 17789971
>>>>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?
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 17790255
>>>>  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
0
 
LVL 8

Expert Comment

by:List244
ID: 17790417
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)
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 17790639
>>>> 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

           
}
0
 
LVL 19

Author Comment

by:mrwad99
ID: 17796777
Thanks both :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now