tags:

views:

63

answers:

5

Hi,

Consider the following:

int a[2];
cin >> a[0] >> a[1];

const int D = a[1] - a[0];

cout << D << "\n";

a[1] = 5; a[0] = 2;

cout << D << "\n";

I'm a bit confused now. Why does it print the same value for D? Why doesn't changing the array values change the value of D? At what point in time is the value of D determined and stored?

Thanks!

+2  A: 

The value of D is determined in the line where you assign it. It can't change just because the values in the expression you used to calculate it changes, even if it weren't declared const. (Few programming languages have variables that work like Excel spreadsheet cells: the cost of recomputing everything would be unpredictable but significant.)

If you want the value of D to change, you have to explicitly assign a new value, which the compiler won't let you do unless you remove the const.

Pontus Gagge
A: 

the value of D is calculated and stored in this line :

const int D = a[1] - a[0];

D is now a variable with it's own value. D being const, it's value will never change (without hacking).

C++ variables (and any programming langage variable) are not defined as relation to other variables. They are defined as changeable (or not) values.

BatchyX
There are some languages where it would change. prolog kinda-sorta does this. Pass-by-name-value will do similar things, too.
JoshD
A: 
const int D = a[1] - a[0];

Right there. At that point the value of D is determined from the current value of a[1] and a[0] and stored. It is then disconnected completely from a[1] and a[0]. Changes to those two will no longer have an effect on D.

In the vast majority of programming language this is exactly how it will work. The = operator is called the assignment operator. It takes the result of expression on the right side and assigns it to the variable on the left side. It works by value, not by reference. So it will assign only the value at the time of execution to the variable. The variable will not change with out a second assignment.

There are cases in C++ and a few other languages where this is not strictly true, they deal with pointers. And will look like this:

int b = 5;
int *d = &b; 

The expression on the right side is the address of a (single) variable that gets assigned to a pointer (the & operator is the address operator, the * operator on declaration declares that d is a pointer). The pointer then contains the address of that variable. The value it gives when dereferenced with be the value of the variable. However, pointers only hold the value of a single variable. They cannot be an alias to an expression. But before you dig further into pointers, you should get yourself a better grasp of the language in general. Pointers are pretty complex and advanced topic.

Daniel Bingham
+1  A: 

You could create a special class for D that reevaluates when the values it refers to change.

class Diff
{
    int & first;
    int & second;

public:
    Diff( int & a, int & b ) :
      first( a ), second( b )
    {
    }

    int get() const
    {
       return first - second;
    }
};

std::ostream & operator<<(std::ostream & os, const Diff & diff )
{
   return os << diff.get();
};

Your class here relies on the two values it has to remain live, i.e. if one of them goes out of scope you will have bad references in your class. You could overload operator int() in place of get() although implicit conversion overloads are usually considered bad practice.

CashCow
A: 

Here's a counter-question that should help you with thinking about how this works:

int a[2];
cin >> a[0] >> a[1];

int D = a[1] - a[0];
D = 6;

Now, after changing D, should a[0] and a[1] be 1 and 5, 2 and 4, or 3 and 3 respectively?

If that makes no sense, then why should it work the other way around?

Kaz Dragon