Any way to have the return string alias with the defined name in caller scope?

How to have the return string alias can be 'seen' with the defined name in caller scope, without being as global nor passing it off ?
illustration is sort of:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using namespace std;
string& first(string& s);

int main(){
  string m("Hello");
  string return_str, result;

  result = funct(m); // or
                        //cout << first(m);
}


string& first(string& s){
	
	return_str=s + " World";
	return	return_str;

}

How to solve such by making use of best C++ features ?
Last edited on
It isn't a question of using "best C++ features, " understanding scope is a basic concept.

A couple of ways to solve a scope issue, without globals or passing a pointer/reference into a function for a return:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

std::string& func(std::string&);

int main()
{
   std::string str("Hello");

   std::string result { func(str) };

   std::cout << result << '\n';
}


std::string& func(std::string& str)
{
   std::string ret_str { str + " World" };

   // this returns a temp string, this leads to undefined behavior
   // very bad, do not do it!
   return ret_str;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

std::string& func(std::string&);

int main()
{
   std::string str("Hello");

   std::cout << func(str) << '\n';
}


std::string& func(std::string& str)
{
   return str += " World";
}


A bit of a refresher on what scope is:
https://www.learncpp.com/cpp-tutorial/4-1a-local-variables-and-local-scope/
Last edited on
Note that returning a local variable as shown in Furry Guy's first example invokes undefined behavior and thus a possible crash. It's simply wrong.
My first snippet knowingly fixed part of a scope issue and traded it for a problem with undefined behavior based on scope.

I thought I'd mentioned that earlier, it was part of the post before I hit submit. I see now it didn't take.
@Furry Guy
You may add a comment (at least line 20 of your first example) to make clear that it is an erroneous return. This kind of bugs are hard to detect.
You just need to remove the return by reference so you get a copy back, don't you?

1
2
3
4
std::string func(std::string& str)
{
   return str += " World";
}


Additionally, are you allowed to move local variables? I'm guessing not because the memory "stolen" will be on the previous stack and thus invalid once the called function returns?
I did try to retroactively add a comment, and the first and 2nd attempt flaked.

My 'net connection tonight/this morning has been acting up.

Not trying to use that as an excuse, though. It was and is my screw-up in the first place.
You just need to remove the return by reference so you get a copy back, don't you?

I don't understand why you want to return a string at all here. You're passing str by reference, so whatever the calling code passes in will already be modified.

What purpose is there in also returning the modified string?
I am responsible for all your vexations, so say sorry.. it's just a notion on "modern" programming language, is there one such feature ?
When I wrote first, on mind was friend or some new C++17 breakthrough or whatever useful brilliant idea by the committee...
Thanks all.
Last edited on
Topic archived. No new replies allowed.