| references/const-ref-t-2.cc - Illustrates that it is not good to return a const reference to a deallocated local variable. | Lecture 2 - slide 16 : 29 Program 4 |
// Illustrates that it is dangerous to have a (const) reference to a deallocated variable.
// Causes a warning from the compiler.
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
typedef int T;
const T& f(double d){
double e = 2 * d;
return e; // Compiler warning:
// returning reference to temporary
}
int main(){
double expression = 5.3;
const T &var = f(expression);
cout << var << endl; // ??
}