operator overloading... I'm stuck

This is a discussion on operator overloading... I'm stuck within the C++ Programming forums, part of the General Programming Boards category; How would I overload the '=' operator so that I can have ooint = int; Would the following work? Code: ...

  1. #31
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    How would I overload the '=' operator so that I can have

    ooint = int;

    Would the following work?

    Code:
    ooint operator = ( const ooint & lhs )
    {
        int hold = lhs.converttoint();
    
        char s[10];
        sprintf ( s, "%d", hold );
    
        return ooint ( s );
    }

  2. #32
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Would the following work?
    No, because you'er passing an object of ooint, not an integer. Try taking an int, converting it to a C string, and calling operator= that takes a C string:
    Code:
    const ooint & ooint::operator = ( int rhs )
    {
        char s[10];
        sprintf ( s, "%d", rhs );
    
        return operator= ( s );
    }
    -Prelude
    My best code is written with the delete key.

  3. #33
    GuestTrauts
    Guest
    I thought you had to have a member of the class type, though?

    Thanks so much, I'll try that when I get home.

  4. #34
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I thought you had to have a member of the class type, though?
    Only if the overloaded operator is global. If the operator is a member function then the left hand side is automatically *this.

    -Prelude
    My best code is written with the delete key.

  5. #35
    GuestTrauts
    Guest
    I'm in your debt, Prelude. Thank you so much! I'll post the class when I'm done.

    Besides weird manipulations, it will let you bypass the atoi and itoas. I think that that would be very convenient because I hate that.

  6. #36
    GuestTrauts
    Guest

    One more problem!

    const ooint & operator += ( const ooint & str ); // append str
    const ooint & operator += ( const char * s); // append s
    const ooint & operator += ( char ch ); // append char

    Those append (they're from the original header), and I want them to act as if they were integers. Problem is, I'm not quite sure how to go about doing this.

    I need 5 possibilities: as above, only not appending (i.e.: a+=1 means a = a + 1 not strcat(a,"1"))

    const ooint & operator += ( const ooint & str );
    const ooint & operator += ( const char * s);
    const ooint & operator += ( char ch );
    const ooint operator + ( const ooint & lhs, int rhs );
    const ooint operator + ( const ooint & lhs, const ooint & rhs )

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

  7. #37
    GuestTrauts
    Guest

    Unhappy

    Operator/Operation Performed
    *= Multiplication assignment
    /= Division assignment
    %= Remainder assignment
    += Addition assignment
    -= Subtraction assignment
    &= Bitwise-AND assignment
    |= Bitwise-inclusive-OR assignment
    ^= Bitwise-exclusive-OR assignment

    I need all of those with 5 each (all possible combinations), and I have no clue

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  2. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 04:44 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 11:40 AM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 07:37 PM
  5. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 08:28 PM

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