| references/const-ref-t-3.cc - A similar program that initializes a user defined struct via a 'functional casting' constructor. | Lecture 2 - slide 16 : 29 Program 2 |
// Another example of type conversion (to a user-defined type) before the binding of the const reference.
#include <iostream>
#include <string>
#include <cmath>
struct TwoDoubles{
double f1, f2;
TwoDoubles(double d): f1{d/2}, f2{d/2}{ // A TwoDobles object can be constructed from a double
}
};
int main(){
using namespace std;
const TwoDoubles &var = 6.4; // A temporary TwoDoubles object is made, by
// activating the Twodoubles constructor on 6.4.
// var becomes a const reference to the
// TwoDoubles object.
cout << var.f1 << ", " << var.f2 << endl; // 3.2, 3.2
}