void outputString(const string &ss) {
cout << "outputString(const string& ) " + ss << endl;
}
int main(void) {
outputString("constant tranformed to reference argument");
//! outputString(new string("abc")); new only return pointer to object
return 0;
}
Since its prohibited to create temporary object reference transforming to methods,this syntax should be useless but even make things more confusing.So why do C++ bother to support this kind of syntax?
EDIT:To be honest,I didn't understand your representation.Considering the above example,we would normally use void outputString(const string ss)
instead of void outputString(const string &ss)
.I think the normal thing is 'pass by value' methods deal with the constants/variables and 'pass by reference' methods deal with variables only.The only reason we should use const type-id &
instead of const type-id
for constants is efficiency because 'pass by reference' methods only take the pointers(addresses) of the primitives constants/objects variables but 'pass by value' methods need to do the copy.
thanks.