I have mentioned this before, I have to use turbo c++ in class.
Problem:
I wrote the following code & to my surprise the code passed the compilation without any problem. I was even more surprised with the output.
I just don't understand whats happening. This code won't compile in any decent compiler.
The Code:
#include<iostream.h>
class ref
{
public:
int a ; double b ;
ref ( int A, double B ) { a = A, b = B ; }
} ;
int main()
{
const ref obj ( 5, 5.5 ) ;
ref &b = obj ;
const int i = 5 ;
int &I = i ;
b.a = 10, b.b = 10.1 ;
I = 10 ;
cout << "Object: " << obj.a << " " << obj.b
<< "Its Reference " << b.a << " " << b.b ;
cout << endl << i << " " << I ;
cin.get() ;
return 0 ;
}
Output:
Object: 10 10.1Its Reference 10 10.1
5 10
Question:
Even though turbo C++ may be an old compiler, why these errors?
1) I was able to modify the const object of the class ref
2) if 'I' is just the reference of 'i' then how come a memory location store 2 values 5 & 10.
Please comment on this.