Clarification on references with class object?

I was reading up on references with class object and I can't seem to understand something....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  #include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

string version1(const string & s1, const string & s2);
const string & version2(string & s1, const string & s2);  // Has Side Effect
const string & version3(string & s1, const string & s2); // Bad Design

int main()
{
	string input;
	string copy;
	string result;

	cout << "Enter a string: ";
	getline(cin, input);
	copy = input;
	cout << "Your string as entered: " << input << endl;
	result = version1(input, "***");
	cout << "Your string enhanced: " << result << endl;
	cout << "Your original string: " << input << endl;
	
	cout << endl;

	result = version2(input, "###");
	cout << "Your string enhanced: " << result << endl;
	cout << "Your original string: " << input << endl;
    
	
	system("Pause");
	
	cout << "Resetting original string.\n";
	input = copy;
	result = version3(input, "@@@");
	cout << "Your string enhanced: " << result << endl;
	cout << "Your original string: " << input << endl;

  system("Pause");
  return 0;
}

string version1(const string & s1, const string & s2)
{
   string temp;

   temp = s2 + s1 + s2;
   return temp;
}
const string & version2(string & s1, const string & s2) // Has Side Effect
{
  s1 = s2 + s1 + s2;
  // Safe to return reference passed to function
  return s1;
}
const string & version3(string & s1, const string & s2) // Bad Design
{
   string temp;

   temp = s2 + s1 + s2;
   // Unsafe to return reference to local variable
   return temp;
}


What is the difference between Version1() and Version3(). The only difference I see is the const parameter in Version1(). The book I am reading says the reason why version3() is wrong is because it creates string temp in Version3() as a local variable. As the function terminates the temp variable no longer exists. Wouldn't that also apply to the Version1() as well? I know Version1(), has const string & s1 as a parameter, but I don't understand how that can make a difference. If somebody can clarify this for me, I would appreciate it.
The bigger difference in version1 and version3 is the return type. version1 returns a string. version3 returns a reference to a string. So, version1 returns a copy of temp, which is good, since temp stops existing when the function ends, and version2 returns a reference to temp, which is bad, since temp stops existing when the function ends.
Last edited on
@cire, thanks for the reply. You mentioned version1 returns a copy of temp, but doesn't it have references as parameters. Meaning its already accessing memory from the input (found in main) variable? I thought that what the difference between passing by reference and passing by value. Sorry for the silly question. Just confused.
@cire, thanks for the reply. You mentioned version1 returns a copy of temp, but doesn't it have references as parameters. Meaning its already accessing memory from the input (found in main) variable? I thought that what the difference between passing by reference and passing by value.

Yes, that's true. But cire wasn't talking about the parameters being passed into (and back out of) the function. He was talking about the return value.

In version1, the function returns a complete instance of a string, which is copy of temp, which is fine.

In version3, the function doesn't return a complete string - it simply returns a reference to temp. However, by the time the calling code receives the return value, the function has exitted and temp has been destroyed. This means the reference is no longer valid - because the thing it's referring to has been destroyed.
Last edited on
Topic archived. No new replies allowed.