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,182
1
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?
 

iSee

macrumors 68040
Oct 25, 2004
3,540
272
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.
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
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
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
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.
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
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
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
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.