?
Solved

string class argument

Posted on 2004-04-26
5
Medium Priority
?
345 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
[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
  • 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 500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
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 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++.

649 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