Basic pointer question...

Hello,

I have a very general programming question about pointers. I have an integer variable describing the position of an object, x. What I'd like to do is have another variable that takes x and subtracts a constant number off of it. For example, I want a variable y that is equal to x-5. The problem is that I want y to always reflect the current position x without having to call it every time.
I don't want to do this every time I use y:

1
2
y=x-5;
function(y);


Is there any way to do this, maybe using pointers?

My initial thought was trying this:

1
2
3
4
5
6
int x;
int* y;
int z;

y = &x;
z = *y - 5;


But then I realized that it doesn't work.

I know that I could type in x-5 in every instance instead of defining a variable y, but x-5 is a meaningful quantity in my program that has a specific name, and I'd like the code to reflect that by having it assigned its own variable.

Thanks!
1
2
3
4
5
int y(int a) {
  return a - 5;
}
//
function(y(x));


1
2
3
4
5
6
7
8
9
10
class Y {
  int & a_;
public:
  Y( int & a ) : a_( a ) {}

  operator int() { return a_ - 5; }
};
//
Y y(a);
function(y);
That makes sense, thanks!
Topic archived. No new replies allowed.