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

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    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. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    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...

    so then overloading the + and other signs?

    Does this sound like it would work?

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    >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 [edit] 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
    Last edited by *ClownPimp*; 11-21-2002 at 11:58 AM.
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    > 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
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Redefining arrays
    By FlyingShoes in forum C Programming
    Replies: 1
    Last Post: 06-25-2009, 12:16 PM
  2. redefining variables without extern
    By robwhit in forum C Programming
    Replies: 16
    Last Post: 05-23-2008, 02:24 PM
  3. Redefining Structs
    By Rune Hunter in forum C++ Programming
    Replies: 3
    Last Post: 02-16-2006, 06:55 PM
  4. Errors with redefining objects
    By suzakugaiden in forum C++ Programming
    Replies: 2
    Last Post: 02-06-2005, 08:33 PM
  5. redefining and overriding members
    By strobe9 in forum C++ Programming
    Replies: 3
    Last Post: 01-09-2002, 03:43 PM