Solved

const function arguments: style question

Posted on 2006-10-23
9
239 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

776 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