Passing string as parameter by reference
Global Variables
While string primitives are not passed by reference, one option not mentioned is the ability to use global variables. Of my own conscience, I must advise against this, but not knowing your use case you should know of your options:
s = 'a' // created as a global variableappendSeparating('b', '|') // function now takes only two argumentsconsole.log(s) // global variable affected
function appendSeparating(s2, separator) { // s is a global variable if (typeof s === 'undefined') return;
if (s != "") s += separator; s += s2;}
Passing strings by reference and value in C++
Your code works as expected.
&filename
returns the memory address of (aka a pointer to) filename
, but startup(std::string& name)
wants a reference, not a pointer.
References in C++ are simply passed with the normal "pass-by-value" syntax:
startup(filename)
takes filename
by reference.
If you modified the startup
function to take a pointer to an std::string
instead:
void startup(std::string* name)
then you would pass it using the address-of operator:
startup(&filename)
As a side note, you should also make the outputfile
function take its parameter by reference, because there's no need to copy the string. And since you're not modifying the parameter, you should take it as a const
reference:
void outputfile(const std::string& name)
For more info, here are the rules of thumb for C++ regarding how to pass function parameters.
Why a String object has to be passed by reference?
So, if I modify the value of a String inside a method, it should be modified when the method call is terminated (since String is an object in C#). But this isn't true, in fact if I write:
You're not modifying the String
object. You're modifying the parameter to refer to a different String
. That's just changing a local variable, and is not seen by the caller.
The String reference is being passed by value, just like normal. You need to distinguish between changing the value of the parameter, and modifying the object that a parameter value refers to. You'll see exactly the same behaviour with your own class, even if it's mutable:
class Person
{
public string Name { get; set; }
}
class Test
{
static void Main()
{
var p = new Person { Name = "Tom" };
Method(p);
Console.WriteLine(p.Name);
}
static void Method(Person parameter)
{
parameter = new Person { Name = "Robin" };
}
}
Now if in Method
you made a change to the object instead, e.g.
static void Method(Person parameter)
{
parameter.Name = "Robin";
}
... then you'd see a change to the output. But that's not modifying the parameter. The only reason that string's immutability is relevant is that it means the second version of Method
above has no equivalent (in safe code) when the parameter is a string.
See my article on parameter passing for more details.
Should I pass a string by value or pass a pointer to it?
It depends on what you want the behavior of the code to be.
Most of the suggested answers here change the behavior from your posted code in that they will modify the string that's passed in (or make the code fail to compile because it can't modify the passed in argument).
In the example you posted,the string passed to myclass::setVersion()
will not be modified (the parameter may be modified, but that is just a copy fo the string passed in; a copy which will be destroyed when the function returns).
For a a case like this, I'd suggest passing a const std::string&
:
int myclass::setVersion(std::string const& ver)
{
if (ver.size()>1)
{
version = ver;
return 0;
}
else
return -1;
}
This way the copy is made only when necessary.
But honestly, unless the function is called often, it's probably nothing to worry much about.
How is String a reference type in Java?
In the first case you are calling a method to the referenced object, thus the referenced object changes, not the 2 references:
In the second case you are assigning a new object to the reference itself, which is then pointing to that new object:
Related Topics
Aes Gcm Implementation with Authentication Tag in Java
How to Install Rjava in R 3.0 in Ubuntu 13.04
How to Run an R Program from Java
Error: Gradle: Execution Failed for Task ':App:Predexdebug'
Android Retrofit Design Patterns
Loading Data Bigger Than the Memory Size in H2O
How to Fix Install_Parse_Failed_Manifest_Malformed in My Android Application
Theme.Appcompat.Light.Darkactionbar - No Resource Found
How to Get Java Output with Ruby
How Run Cookbook with Dependencies in Chef
Serializing and De-Serializing Android.Graphics.Bitmap in Java
How to Declare on UI Component in Android with Kotlin
What Does Transitive = True in Gradle Exactly Do (W.R.T. Crashlytics)