Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old June 21st, 2016, 12:29 PM
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2016
Location: India
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Question c++ basics

I am new to C++. I don't understand the difference between passing string to a function as a const reference and without the use of const reference.

And also the use of const and reference operator together. Please help

And this is my first post in any forum so excuse me if I made any mistakes.

Last edited by Varun95; June 21st, 2016 at 12:36 PM.
Reply With Quote
  #2 (permalink)  
Old July 10th, 2017, 02:37 PM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2017
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
Originally Posted by Varun95 View Post
I am new to C++. I don't understand the difference between passing string to a function as a const reference and without the use of const reference.
First let's go over what the
Code:
const
and
Code:
&
(reference) key words are and do. The const keyword prevents and operation or function from modifying the variable, in your case the string, that has the const keyword attached to it. This can be helpful when you're passing strings into functions. There will be times when you don't want a function to modify the contents of your string variable, so you can put const in front of the variable name.
ex:
Code:
void showValues ( const string name) 

 { 
     //Code that does stuff with your string variable
}
See how I put the constin front of the name variable? Now if I write code that tried to modify the variable, I'd get an error at compilation time that wouldn't allow me to build the program. You might be wondering why this is important to do. Well if you build a program for a company and then five years later you leave that company and a new programmer has been given the task of updating your code, they'll need this. They won't be as familiar with your code as you were because you created it. Without the const they might write a function that edits the string even though you don't want what's in that string to be edited. It must stay constant so you add a const.


Now for the & or also known as the reference symbol. When you put the & symbol in front of a variable you've turned that variable into a reference variable. When you do that it means that when it's used as a parameter it gives the function access to the original argument. Any changes made to the reference variable are actually performed on the argument referenced by the variable.


Now to be honest I am not 100% sure when you'd pass a string by const reference, since the idea of a reference variable is to change the contents of the variable and the const is to keep the contents of a variable the same . There is sort of a similar application later on in C++ when you're talking about pointers and classes, but I think that's above your head for now. If you could tell me what you've been doing in class it would help me understand where you're coming from. Cheers
Reply With Quote
The Following User Says Thank You to Eureka117 For This Useful Post:
michellejohnso1 (March 5th, 2018)
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
SSIS Basics bharathkumarbs.sidd BOOK: Expert SQL Server 2005 Integration Services ISBN: 978-0-470-13411-5 0 October 25th, 2011 10:16 AM
About JSP basics manoj.shakya J2EE 0 December 11th, 2010 02:33 AM
Need some help on basics jagan7 Java Basics 3 September 9th, 2007 02:44 AM
Basics Andy122 Dreamweaver (all versions) 3 January 2nd, 2005 02:36 PM



All times are GMT -4. The time now is 06:20 PM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.