Operator overloading

This is a discussion on Operator overloading within the C++ Programming forums, part of the General Programming Boards category; My last thread died, so I'll start anew. http://cboard.cprogramming.com/showt...threadid=30087 I'm storing an integer inside a class that is similar to ...

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

    Operator overloading

    My last thread died, so I'll start anew.

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

    I'm storing an integer inside a class that is similar to a string class. I'm overloading every operator that comes to mind so that it functions exactly the same as an integer. I've got most of the operators working, but there are a few that stump me:

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

    If any of those don't normally correspond to an integer, then I need to know how to get it to act as a string.

    Thanks so much for your help!

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Here's a reference that will probably help you out:

    http://www.stud.fim.ntnu.no/~oystesk/CPP/index.htm

    You can review overloading ++/-- operators in day 10 and bitwise operators in day 21. That should help you on your journey.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I'll look into that, thanks

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    It doesn't seem to be working.

    void operator++ () { itoa(++converttoint(),CString,10); }

    that give me this:
    error C2105: '++' needs l-value

    I don't know how to fix that.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    read further. The version you tried is just the beginning form discussed by the author. It isn't very useful, as it has a major flaw, which you found and he discusses. It get's better as you read further. So go back to the reference again.

    In general, the next step is to decide whether the ++ operator will be used in prefix form (++whatever) or postfix form (whatever++). The postfix form is actually defined in terms of the prefix form so do that first. The reference I gave you talks all about it.

    if myInt is the embedded int member of the ooint class, the next step to creating a full blown overloaded prefix operator for your class might be something like this:

    //crude prefix increment operator for an ooint class
    ooint operator++ ()
    {
    ++myInt;
    return *this;
    }

    There is still some stuff to flesh this out, too, but it's all in the reference. Keep at it.

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I know that the code I tried was for ++i, not i++.

    I want to overload it both ways.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Thank you though.

    Originally posted by elad


    //crude prefix increment operator for an ooint class
    ooint operator++ ()
    {
    ++myInt;
    return *this;
    }
    It's stored as a string, so I'd have to use itoa(++converttoint(),CString,10);

    [EDIT]
    Fixed that, ++ now works:
    Code:
    const ooint & ooint::operator++ () 
    { 
    	int myInt = converttoint();
    	++myInt;
    
    	char *s = itoa(myInt,CString,10); 
    
    	int len = 0;                         // length of newly constructed string
        assert(s != 0);                      // make sure s non-NULL
        len = strlen(s);                     // # of characters in string
    
        // free old string if necessary
    
        if (Capacity < len + 1)
        {
    	delete[] CString;	// delete old string
    	Capacity = len + 1;	// add 1 for '\0'
    	CString = new char[Capacity];
        }
        strcpy(CString,s);
    	return *this;
    }
    Thanks so much Elad!
    Last edited by Trauts; 12-10-2002 at 09:35 AM.

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Originally posted by elad

    You can review overloading ++/-- operators in day 10 and bitwise operators in day 21. That should help you on your journey.
    I just read through day 21, and it doesn't mention overloading the operators. All it does is explain what they do. So I still can't overload these:

    &=
    |=
    ^=
    &
    |
    ^

    Do you know of any other tutorials?

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