简体   繁体   English

c ++重载另一个类的赋值运算符

[英]c++ overloading assignment operator of another class

i have a c++ class to handle fractions and i want it to allow conversion to double, 我有一个c ++类来处理分数,我希望它允许转换为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? 我究竟做错了什么?

您需要的是转换运算符:

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

If you want the ability to assign a double to fraction you can declare a non-explicit constructor: 如果您希望能够将double分配给fraction ,则可以声明非显式构造函数:

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

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. 由于double不是类,这意味着你无法为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. 话虽如此,这意味着你可以在需要double,int,float的地方使用一个分数,因为编译器使用运算符进行隐式转换,不仅要加倍,还要转换为int和其他可能从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: 在C ++ 11中,有可能使运算符显式:

  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. 一个小旁注 :你应该作出这样的转换功能常量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM