| A similar program that initializes a user defined struct via a 'functional casting' constructor. | Lecture 2 - slide 18 : 42 Program 2 |
// Shows a type conversion before the binding of the const reference.
#include <iostream>
#include <string>
#include <cmath>
struct TwoDoubles{
double f1, f2;
TwoDoubles(double d){ // A TwoDobles object can be constructed from a double
f1 = d/2; f2 = d/2;
}
};
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
}