Solved

const keyword in functions

Posted on 2006-11-18
3
328 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

830 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