-
String in C++
Hi guys!
String in C++ is just an object of class, in C we were using
and all fine with that also over C++ support that.
but what's confusing me, if I'm using string data type as String , in other words using the facility that C++ provide, why while passing string arguments I must write
Code:
Fun(String &argument)
and not just write
Code:
Fun(String argument)
?
I'm really confused about that ! , I'm making an analogy to
String <=> char * , so why I need while calling function by String value to identify the argument of the function that's type String as &argument ?
for example:
Code:
void(std:: const String &argument)
{
cout<<argument;
}
-
C++ Syntax for $400, What is a reference?
References - C++ Reference - Cprogramming.com
Without the & in the function declaration, the String object is copied. When passed by reference, it isn't. Also, what the function may modify in the string is still in scope after the function returns. It is similar to passing by pointer in C, but has very different implications.
One implication is that in all but a few contrived circumstances, the reference can't be nullptr. If a pointer were used, the pointer might be nullptr, but with references it should be possible to assume it isn't.
Now, there are obfuscations in C++ people use to try to show how it can be made nullptr, but you have to actually work at it.