Overloading the "=" Operator in C++

Discussion in 'Mac Programming' started by bobber205, May 10, 2007.

  1. bobber205 macrumors 68020

    bobber205

    Joined:
    Nov 15, 2005
    Location:
    Oregon
    #1
    I want to do a

    integer = class

    I tried creating this function.

    Code:
    int Number::operator= (const Number rightvalue) 
    {
     
    return rightvalue.self;
    }
    
    However when I try to do a
    Code:
    int temp(0);
    temp = mynumber;
    
    I get this
    Code:
    error C2440: '=' : cannot convert from 'Number' to 'int'
    1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    
    I am pretty new to operator overloading but I understand the basic concepts.

    Any hints?
     
  2. iSee macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #2
    The best way to do this is to implement the "cast to int" operator for the Number class, not the = operator. The syntax is like this:

    Code:
    ...
    Number::operator int()
    { 
        return this.self; 
    }
    ...
    
    Notice that you don't specify a return value (kind of like a constructor). That would be redundant, obviously.
     
  3. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #3
    iSee is right, you need to use the cast operator.

    However the assignment operator you defined above actually does work but in a convoluted way, eg

    Code:
    int v ;
    Number a( 10 ) ;
    Number b( 20 );
    
    v = a = b ; // This assign the value of b to v
    
    imho you're best using the assignment operator for assigning values to Number, eg

    Code:
    const Number Number::operator= ( int value ) 
    {
        self = value;
        return *this ;
    }
    
    const Number Number::operator= (const Number & rightvalue) 
    {
        self = rightvalue.self;
        return *this ;
    }
    …
    …
    Number a, b, c ; // Use the assignment operators now!
    a = 16 ;
    a = b ;
    a = b = c ;
    
    hope this helps!

    b e n
     
  4. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #4
    operator= is a very special case, so what I will suggest might not work.

    First of all, a function Number::eek:peratorXXX (T yyy) will be an operator XXX which expects a Number on the left side and an object of type T on the right side. Since you want an int on the left side, this cannot work. And actually, since you want an int on the left side and not some class, a member of a class cannot possible work.

    Try writing a plain function (not a class member)

    int operator= (int& leftside, const Number& rightside)

    then declare it to be a "friend" inside class Number. leftside must be an int& because you want to modify it. rightside _should_ be a Number& because otherwise it will be copied which is quite pointless. I am quite sure this method will work for most operators, but as I said, operator= might be handled differently because it is a very special operator.
     
  5. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #5
    That won't work. The assignment operator has to be a member function and only takes one argument.

    Using the cast operator is easy, eg

    Code:
    class Number
    {
      …
      public: operator int () const { return value_ ; }
      private: int value_ ;
    } ;
    …
    …
    int value ;
    Number a( 30 ) ;
    value = a ;
    
    b e n
     
  6. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #6
    You're right; operator=, operator[] and operator-> must be member functions; all other operators can be member functions or non-member functions.
     

Share This Page