string class argument

Posted on 2004-04-26
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";


void foo(string s);

Question by:el_rooky
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

Expert Comment

ID: 10919503
you are creating a copy of myStr. Its passed by value

Expert Comment

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

Accepted Solution

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";


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 :-)

Author Comment

ID: 10919907
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;

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;


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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

688 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