Reference is not alias
#include <iostream> class cmplx { public: double x; double y; double& real; double& imaginary; public: cmplx(double _x, double _y) : x(_x), y(_y), real(_x), imaginary(_y) {} }; int main(int argc, char** argv) { cmplx c(0.1, 0.1); std::cout << c.real << "+i" << c.imaginary << std::endl; }
Following the discussion in http://www.thescripts.com/forum/thread640395.html,
I can say that making aliases for a member variable using reference is not a good idea.
Since, in run time, those aliases *may or may not* be deleted.
In the above example, the type cmplx may consume sizeof(double)*2 + sizeof(double*),
or sizeof(double)*2.
#include <iostream> class cmplx_simple { public: long double x; long double y; public: cmplx_simple(long double _x, long double _y) : x(_x), y(_y) {} }; class cmplx { public: long double x; long double y; mutable long double& real; mutable long double& imaginary; public: cmplx(long double _x, long double _y) : x(_x), y(_y), real(x), imaginary(y) {} }; int main(int argc, char** argv) { cmplx_simple x(10.0, 10.0); cmplx c(0.1, 0.1); std::cout << sizeof(x) << std::endl; std::cout << sizeof(c) << std::endl; std::cout << c.real << "+i" << c.imaginary << std::endl; c.x = 0.2; c.y = 0.3; std::cout << c.real << "+i" << c.imaginary << std::endl; std::cout << &c.real << " " << &c.x << std::endl; }