Solved

const keyword in functions

Posted on 2006-11-18
3
327 Views
Last Modified: 2010-04-01
Hi Experts,

when functions are declared with const parameters, I think I read it means it will  not be changed within that function.

functionOne( const string& a);    
functionTwo( const Test& t);
funcionThree(const bool c);

Are these always good idea when I am not going to change that parameter inside the function?

Also, functionOne(const string a)  is it going to be the same as functionOne(const string& a)
Other than that I am passing reference rather than value.

0
Comment
Question by:ambuli
3 Comments
 
LVL 9

Assisted Solution

by:DrAske
DrAske earned 100 total points
ID: 17973905
>>Are these always good idea when I am not going to change that parameter inside the function?
yes :)

>>Also, functionOne(const string a)  is it going to be the same as functionOne(const string& a)
regarding that *string* a will not change inside the function, *YES* it is.
but the first argument is passed by-value, and the other one is passed by-reference
(using  *&* depends on the size of the arguement)

NOTE!!
Many C++ programmers prefer that
1) Modifiable arguments be passed to functions by using pointers.
2) SMALL NONmodifiable arguments be passed by value.
3) HUGE (big) NONmodifiable arguments be passed by using REFERECES TO CONSTANTS.

regards,Ahmad;
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 150 total points
ID: 17973949
functionOne( const string& a);    
functionTwo( const Test& t);

These two functions get reference parameter. const definition ensures that parameters cannot be changed. Using const here is OK.

funcionThree(const bool c);
functionOne(const string a);

In this case using const doesn't make sence. Parameters are passed by value, this means, function operates with copy of these parameters. For caller, it doesn't matter whether such parameter is const or not - caller's parameter is not changed in any case.
0
 

Author Comment

by:ambuli
ID: 17975706
Thank you.  Now, everything makes more sense :-)
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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…

803 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