I am a little confused on pass string by reference. As in func3, will destructor of string be called at the end of func? Will value of var be undefined after func3 is called?
I believe the destructor will be called at the end of func3, invalidating references in func2 (but I may be wrong, I'm no expert). This means that the value of var will probably be undefined.
What do you mean by immutable?
i saw codes like this which has been working perfect for a long time. I am a little bit lost on this.
For immutable string, I mean we cannot really alter the string after it is assgned, so maybe func1 and func2 are doing same thing. I may mess up with some other concepts from different languages.
Also, what if using int instead of string in the above functions?
The destructor for string will be called for variable s at the end of func3 - However, this will have no effect on var - because var copied the string into itself here var = myvalue;
**Note about references:**
Once a reference is initialized - any further action using that reference is carried out on the variable being referenced NOT on the reference it self.
This means that in func2 var = myvalue; is assigning the object being referenced by myvalue - that is the string s object back in func3 - to the var variable.
This is different from pointers which have to be explicitly dereferenced.
Same deal with integers, AFAIK. Strings are designed to operate like a plain old datatype.
And why can't you alter the string after it is assigned? Don't you think that question is a little... situational?