What is the best way to pass these objects to a function?

I'm a Java programmer who's looked into C++ from time to time, but I'm no expert.
I've been looking into C++11 and I came across the R-value Reference principle in Move constructors and that completely muddled everything I knew about passing large objects efficiently so I have a couple of questions.

In the code samples below, am I using the best approach to pass the arguments to the functions? If not, could you please explain when to use pass by reference and pass by R-value reference?

I hope this isn't a dumb question.

SAMPLE 1:
1
2
3
4
5
6
7
8
#include <string>

void write(std::string& sentence);

int main()
{
  std::string sentence = "This is an l-value so it's better to pass it by reference when the data type is large?";
}


SAMPLE 2:
1
2
3
4
5
6
7
8
9
#include <string>

void write(std::string sentence);

int main()
{
  write("This sentence is an R-value, and it should be passed to an l-value type");
}


SAMPLE 3:
1
2
3
4
5
6
7
8
9
10
void write(std::string&& sentence);

int main()
{
  write("If i understand right, this is the most effective way"
+"of passing large objects to a function and it works with r-value data such as this sentence");

  std::string sentence = "it also works well with l-value data such as this."
  write(sentence;)
}
1
2
3
4
  void write(std::string&& sentence); 
  // ...
  std::string sentence = "it also works well with l-value data such as this."
  write(sentence;)
'

This won't compile. To get it to work you'd have to do:

write(std::move(sentence)) and if sentence isn't going to be destroyed immediately, that would be a mistake.

Last edited on
could you explain in more detail please? I'm really confused about when's best to use which kind of passing.
Would it be okay if I just stuck with passing by values and passing by references.
Is passing by R-value reference really that important?
Using reference you pass address of data, but not data. So it will be faster and will use less memory, but you can't pass a value
write("This sentence is an R-value, and it should be passed to an l-value type");
this will not work.

1
2
std::string sentence = "This is an l-value so it's better to pass it by reference when the data type is large?";
write(sentence);

this should work.

But passing like this, function can change data in variable, some time you might like it, some times not. If you do not want to change data in variable you uses this
void write(const std::string& sentence);

For large objects it is better to pass by reference.
Last edited on
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
#include <string>
#include <iostream>

//Function takes a reference and change values in variables.
void change(std::string&, std::string&, std::string&);
//Function takes a value.
void change(std::string);

int main()
{
	std::string str1, str2, str3;
	str1 = "String one for change.";
	str2 = "String two for change.";
	str3 = "String three for change.";
	
	//Prints addresses of data.
	std::cout << &str1 << std::endl;
	std::cout << &str2 << std::endl;
	std::cout << &str3 << std::endl;
	std::cout << std::endl;
	
	change(str1, str2, str3);
	
	std::cout << str1 << std::endl;
	std::cout << str2 << std::endl;
	std::cout << str3 << std::endl;
	std::cout << std::endl;
	
	change(str1);
	change(str2);
	change(str3);
	
	return 0;
}

//This function takes addresses of data, but not data. So it works on values given variables stors.
//It do not make copy of data.
void change(std::string& inStr1, std::string& inStr2, std::string& inStr3)
{
	//Prints addresses of data. It is same as above addresses.
	std::cout << &inStr1 << std::endl;
	std::cout << &inStr2 << std::endl;
	std::cout << &inStr3 << std::endl;
	std::cout << std::endl;
	//Change values of variables.
	std::string str = "changed";
	inStr1.replace(7, 3, str);
	inStr2.replace(7, 3, str);
	inStr3.replace(7, 5, str);
}

//This functin takes data. So it works on copy of data stort in given variable.
void change(std::string inStr)
{
	//Prints addresses of data. It is different from above addresses.
	std::cout << &inStr << std::endl;
}

//First function saves time as it do not need to copy data.
//Second function makes a copy of data. And takes more memory, as here are two copys of data in memory. 
Topic archived. No new replies allowed.