# Thread: Redefining an operator... is it possible?

1. ## Redefining an operator... is it possible?

I want to make a class that lets you use an int differently. I already have a string class, so I know operators can be overloaded, but I don't know if thats what I need.

Code:
```ooint x = 500;
x[3] = 1;
if (x == 501)
cout << x << endl << endl;```
Would have the same function as

Code:
```int x = 500;
char y[5];
itoa(x,y,10);
y[3] = 1;
x = atoi(y);
if (x == 501)
cout << x << endl << endl;```
or using x%10, etc. to get the digit.

The question is, how can I get it to read something in [] as though it was calling a function? I don't want it to directly change the digit, I want to use itoa, change it, then atoi. But I don't know how to let me use x[3] that way.

All I need to know is how to get it to take that as an int called Index in the class.

Thanks!

2. I just thought of something...

Instead of using the datatype int inside the class, why not use char and just lots of atoi's? so ooint x = 0; isn't really using an integer in the class, it is a character array...

Does this sound like it would work?

3. >x[3] = 1;

this is kinda tricky to do since the [] operator only takes one argument (two actually, the object '[]' is being applied to and the integer  on second thought, that probably only applies to basic types [/edit]) and you cant define a new combination of operators. But there is a workaround (for defining something that acts like an '[] =' operator) although you would be much better off just creating a function to change a digit in an int

int x = 500;
replaceDigit(x, 3, 1);
if (x == 501)
cout << x << endl << endl;

In general you should avoid giving operators unusual behavior since it makes your code (much) harder to understand

4. > Instead of using the datatype int inside the class, why not use
> char and just lots of atoi's? so ooint x = 0; isn't really using an
> integer in the class, it is a character array...

that will work