Solved

const keyword in functions

Posted on 2006-11-18
3
329 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Embarcadero C++ Builder XE2 TDateTime 8 92
C++ question 3 84
Which Linux flavors will this run on? 6 116
C++ Code Issue 4 48
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

751 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