Solved

string class argument

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

708 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

14 Experts available now in Live!

Get 1:1 Help Now