1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
const std::string getName()
{
return "John Doe";
}
void printName(const std::string &name)
{
std::cout << "Name: " << name << std::endl;
}
int main()
{
// const reference to a temporary object
const std::string &name = getName();
std::cout << "Stored name: " << name << std::endl;
// Passing temporary object to a function expecting a const reference
printName(getName());
return 0;
}
Back to reference