Finishing my "int class"... help with weird operator overloads

This is a discussion on Finishing my "int class"... help with weird operator overloads within the C++ Programming forums, part of the General Programming Boards category; This class stores the data as a string, but the operator overloads make it act like an integer. Problem is, ...

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

    Finishing my "int class"... help with weird operator overloads

    This class stores the data as a string, but the operator overloads make it act like an integer. Problem is, I'm not quite sure how to go about doing this with the remaining operators. They're listed at the bottom of the post.

    I need 4 possibilities each (these are already completed): (i.e.: a+=1 means a = a + 1 )

    const ooint & operator += ( const ooint & str ); // add str to CString
    const ooint & operator += ( const char * s); // add str to CString
    const ooint & operator += ( char ch ); // add str to CString
    const ooint & operator += ( int rhs );

    Something like that, but I still don't have it figured out.

    Operator/Operation Performed
    &= Bitwise-AND assignment
    |= Bitwise-inclusive-OR assignment
    ^= Bitwise-exclusive-OR assignment
    ++ Increment operator
    -- Decrement operator

    I need all of those with 4 each (all possible combinations), and I have no clue how to go about some of them because I don't know how they work.

    operator overloading... I'm stuck has more information on my class if you need it.
    Last edited by Trauts; 12-06-2002 at 08:39 AM.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    How your overload the operators is up to you. For example, what do you want %= to do specific for your data type?

    Kuphryn

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Inside the class, it is stored as a string. Outside the class, it acts like an integer except it allows you to use []

    ooint c = 0;
    char string[] = "100";
    c = c + 5 + string;
    cout << c << skip;

    that gives 105.
    So does this:

    ooint c = "0";
    int integer = 100;
    c = c + 5 + integer;
    cout << c << skip;

    I want the remaining overloads (see the first post) to do what it would normally do with an integer or a string, depending on what data type it normally is used with. I want %= to do what it does for integers, that part is easy, but I need % in order to do that... 5%2=1

    Also, I need ++ and --:

    ooint c = 5;
    c++; //add one to 5
    c--; // subtract one from 5

    [EDIT]%= Remainder assignment
    % Modulus operator

    I figured out those... those ones were obvious[/EDIT]
    Last edited by Trauts; 12-06-2002 at 08:40 AM.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Also, with +, -, *, /, and % operators, I can't add a character like this:

    ooint a = 0;
    a = 1 + '3';

    because I can't declare it in the class without it having a member of type ooint.

    h:\ooint.h(98) : error C2803: 'operator +' must have at least one formal parameter of class type

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21