Solved

string class argument

Posted on 2004-04-26
5
338 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now