Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ overloading assignment operator of another class

i have a c++ class to handle fractions and i want it to allow conversion to double,
i have something like that :

class fraction  
{  
    double n,d;  
public:  
    fraction(double _n, double _d) {n = _n; d = _d;}  
    //some functions  
    double todouble() {return n/d;}  
};  

fraction frac(1,2);  
double dbl = frac.todouble();  

which works fine, but i want to overload the assignment operator so i can go directly with :

double dbl = frac;  

i tried to add this :

double double::operator =(double& dbl, fraction& frac) {return dbl = frac.n / frac.d;}  

which resulted in the following compilation error :

 error: ‘double fraction::operator=(double&, fraction&)’ must take exactly one argument  

what am i doing wrong?

like image 229
Ahmed Abdelfattah Avatar asked Apr 04 '14 09:04

Ahmed Abdelfattah


People also ask

How to overload the assignment operator in C++?

A non-static member function should be used to overload the assignment operator. The compiler generates the function to overload the assignment operator if the function is not written in the class. Overloading assignment operator can be used to create an object just like the copy constructor.

What is operator overloading in C++?

So the main idea behind “Operator overloading” is to use c++ operators with class variables or class objects. Redefining the meaning of operators really does not change their original meaning; instead they have been given additional meaning along with their existing ones. What is the difference between operator functions and normal functions?

What is the use of assignment operator in C++?

The assignment operator (operator=) is used to copy values from one object to another already existing object. The purpose of the copy constructor and the assignment operator are almost equivalent -- both copy one object to another.

How to add two class objects using operator + in C++?

Now, if the user wants to make the operator “+” to add two class objects, the user has to redefine the meaning of “+” operator such that it adds two class objects. This is done by using the concept “Operator overloading”. So the main idea behind “Operator overloading” is to use c++ operators with class variables or class objects.


3 Answers

What you need is conversion operator:

operator double() const { return n / d; }
like image 189
cbel Avatar answered Sep 30 '22 00:09

cbel


You cannot overload an assignment operator as a free function, meaning it must be a class member. Since double is no class, that means you have no way to write an assignment afor double.

The only thing that is left is writing a conversion operator for your class:

class Fraction {
  //...
public:
  double toDouble() const { return n/d; } 
  operator double() const { return toDouble(); } 
};

Having said that, this means you can use a fraction wherever you need a double, int, float, because the compiler uses the operator for implicit conversions, not only to double but also to int and other types that have possible builtin conversion from double. This can be desired for some classes, but it can lead to ambiguities and errors, because one often overlooks the conversion opportuinities the compiler takes into account.

In C++11 there is the possibility to make the operator explicit:

  explicit operator double() const { return toDouble(); } 

This would mean that implicit conversion are not allowed, and copy-initialization won't work, but direct initialization will:

 double dbl = frac;  //Error
 double dbl{frac};   //Ok 

A little sidenote: you should make that conversion function const.

like image 22
Arne Mertz Avatar answered Sep 30 '22 00:09

Arne Mertz


If you want the ability to assign a double to fraction you can declare a non-explicit constructor:

fraction(double d = 0, double n = 1) : d(d), n(n) {}

and for reverse

operator double() const { return n/d; }

and then

fraction f;

f = 12.5;      // Assign a double to fraction

double x = f;  // Assign fraction as double to a double
like image 21
masoud Avatar answered Sep 30 '22 00:09

masoud