Solved

const keyword in functions

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

Independent Software Vendors: 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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

615 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