简体   繁体   English

C ++运算符重载int类型

[英]C++ Operator Overloading Int Types

I'm working on a fractions class in C++ and have defined the addition of a frac and an int in the new class, but it requires them to show up in that order: frac + int. 我正在C ++中的分数类上工作,并在新类中定义了frac和int的添加,但是它要求它们按以下顺序显示:frac + int。 Is there a way to overload + in the Int type such that we have a function that accepts a frac and outputs a frac? 有没有办法在Int类型中重载+,以便我们有一个接受frac并输出frac的函数? Or is there a way to reverse the order in the new class? 还是有办法在新类中颠倒顺序?

I already have frac + frac and frac += frac operators defined. 我已经定义了frac + frac和frac + = frac运算符。 The two functions in question are: 有问题的两个功能是:

frac operator+=(int b)
{
    frac c = { n + b*d, d };
    return c;
}

frac operator+(int b)
{
    frac c = *this;
    frac d = { b };
    c += d;
    return c;
}

something like this: 像这样的东西:

frac int::operator+=(frac& b)
{
    frac c = { this * b.den() + b.num(), b.den() };
    return c;
}

But I'm not sure how to actually accomplish this. 但是我不确定如何真正实现这一目标。


Code from question below: 来自以下问题的代码:

frac &operator+=(frac b)
{
    frac c = { n * b.den() + b.num() * d, d * b.den() };
    n = c.num();
    d = c.den();
    return *this;
}

frac operator+(int a, frac b)
{
    return frac{ a } += b;
}

Well there's no class int so you can't do what you suggested but the easy thing is to declare a function like this (inside your class) 好吧,因为没有类int所以您无法执行您建议的操作,但简单的事情是在类内部声明一个这样的函数

 friend frac operator+(int a, frac b);

That way this function will be called when the other order is used. 这样,当使用其他顺序时将调用此函数。 Edit: you need to use the friend specifier because the function isn't in the class, I forgot about that. 编辑:您需要使用friend说明符,因为该函数不在类中,我忘了。

The proper way to do this is to make frac have an implicit constructor from int, and use a free operator+ so that conversions apply to both arguments. 正确的方法是使frac具有一个int的隐式构造函数,并使用一个自由的operator+以便转换适用于两个参数。 Also you can implement + in terms of += and so on. 同样,您可以按照+=等实现+ Eg 例如

struct frac
{
    frac(int x) { /* initialize frac with x */ }

    frac &operator+=(frac f) { /* add f to this */ return *this; }
};

frac operator+(frac f1, frac f2)
{
    return f1 += f2;
}

Update : some people consider implicit conversion constructors to be bad style; 更新 :有些人认为隐式转换构造函数是不好的样式; if you're one of those then you can work around it by using an explicit constructor and writing a series of operators with exact parameters, eg 如果您是其中之一,则可以通过使用显式构造函数并编写一系列具有精确参数的运算符来解决它,例如

frac operator+(int f1, frac f2) { return frac(f1) += f2; }
frac operator+(frac f1, int f2) { return f1 += frac(f2); }
frac operator+(frac f1, frac f2) { return f1 += f2; }

Declare the member operators as static and you can have two parameters. 将成员运算符声明为静态,您可以有两个参数。 See the declaration of WholeNumber -- the operators are static and have no instance of *this. 请参阅WholeNumber的声明-运算符是静态的,没有* this的实例。

Operators in Cross Compiler Objects within Visual Studio Visual Studio中的交叉编译器对象中的运算符

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

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