Solved

string class argument

Posted on 2004-04-26
5
339 Views
Last Modified: 2010-04-01
I've got a very simple and dumb question. When I pass a string class as an argument to a function as in the code belowed, I am passing a pointer or am I creating a copy of myStr in foo?


#include <string>
#include <iostream>
#include <stdlib.h>

using namespace std;

string myStr = "this is my string";

foo(myStr);
...

//-------------------------------
void foo(string s);
{
}



0
Comment
Question by:el_rooky
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:akalmani
ID: 10919503
you are creating a copy of myStr. Its passed by value
0
 
LVL 3

Expert Comment

by:akalmani
ID: 10919553
I meant constructor of the string class is called and then its passed.
0
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 125 total points
ID: 10919593
Your question is about a very basic, but very important concept: How does data get passed to a function? In C++ you (as the programer) have full control over this. The language gives you three different methods:
Pass by value
Pass by reference - as a pointer
Pass by reference - as a C++ reference

If you don't do anything special, you are always passing by value, which means that the compiler will make a copy of your original value, and pass this into your function. This means two things: A copy needs to be created, which can be time consuming, and the function only works with the copy, so it cannot modify the original variable. If you want to avoid this copying, but still want to make sure that your function cannot (not even accidentially) modify the original data, you can use the const reference:

string myStr = "this is my string";

foo(myStr);
...

//-------------------------------
void foo(const string & s);
{
}


In this case you get all the advantages of a call by reference, but you can make sure that your function does not modify the original myStr.

You can do the same with pointers, but using C++ references is much cleaner and easier to read - you will thank yourself if you need to work on your code after a year :-)
0
 

Author Comment

by:el_rooky
ID: 10919907
khkremer,
Thanks for you answer. One more small question. When you say pass as a pointer you don't mean a pointer to the string class.
For example, can I have a pointer to the string class like this?
string *ptr = myStr;

0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10920131
Yes, you can do this, but this will require that you modify how you work with the string (the first part of your sample code is correct, the second part is not):

string * ptr;  // this only defines a pointer to a string, not the string itself

ptr = new string(myStr);  // this creates the new string object and uses the content of myStr as it's value
// ... do something with the string ...
delete ptr;  // destroy the object and deallocate any resources (e.g. memory)

You can also use something like this:

string * ptr;

*ptr = myStr; // copy the contents of myStr to *ptr (which dereferences the pointer, and therefore is a string)
// ...
delete ptr;

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

777 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