Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bobber205

macrumors 68020
Original poster
Nov 15, 2005
2,183
8
Oregon
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?
 
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.
 
Code:
int Number::operator= (const Number rightvalue) 
{
 
return rightvalue.self;
}

Any hints?

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
 
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?

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.
 
int operator= (int& leftside, const Number& rightside)

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
 
That won't work. The assignment operator has to be a member function and only takes one argument.

You're right; operator=, operator[] and operator-> must be member functions; all other operators can be member functions or non-member functions.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.