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.

Recommended Answers

All 5 Replies

> I just don't understand whats happening. This code won't compile in any decent compiler.
So ask your tutor why they're teaching you crap.

> 2) if 'I' is just the reference of 'i' then how come a memory location store 2 values 5 & 10.
It doesn't.
The cout statement probably looks more like cout << endl << 5 << " " << I ; Being const, the compiler is free to perform a value substitution at compile time. In that respect, simple consts in C++ resemble #defines in C.

As you say, any decent compiler would throw out all those "loss of const" errors.

Thanks for your reply, and I am a little confused with it.

Being const, the compiler is free to perform a value substitution at compile time. In that respect, simple consts in C++ resemble #defines in C.

If the compiler replaces const at compile time, then the statement int &I = 5 ; is completely wrong.
And then, I will leave the 1st question since its the problem of Turbo C++ & not mine.

See "The Design and Evolution of C++" by B.Stroustrup, 3.7 (exactly your situation as an example of initial references incorrectness). Obviously, Turbo C++ implements the initial version of C++ references (without later restriction not to bind non-const refs to const objects).
It's not a safety toy...
Well, so many good compilers have its own glucs...

> If the compiler replaces const at compile time, then the statement int &I = 5 ; is completely wrong.
The integer storage location called i will get updated through the reference, ignoring all the const'ness which goes with it.

It's just that the compiler (which knows the value, and sees the const), doesn't see your incorrect code and just does cout << 5

It you looked hard enough with a debugger, you might see this actually happening.

Thanks Salem & ArkM for your replies.
Unfortunately, the book "The Design and Evolution of C++" costs $44.40. Its too expensive to buy:(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.