1

I'm learning C++ recently and have some basic Fraction class to implement operator overloads on. One of the operators to be overloaded is *.

A quick implementation for multiplying two Fraction types:

Fraction Fraction::operator* (const Fraction &rightOperand)
{
    return Fraction(numerator * rightOperand.numerator, denominator * rightOperand.denominator);
}

And one for multiplying where the right hand operand is an int:

Fraction Fraction::operator* (int rightOperand)
{
    return Fraction(numerator * rightOperand, denominator);
}

Then, because multiplication is commutative, multiplying where the left hand operator should be simply:

// declared as friend
Fraction operator* (int leftOperand, const Fraction &rightOperand)
{
    return rightOperand * leftOperand; // should use the overload above, right?
}

However, in the last function, the '*' gives an error:

no operator "*" matches these operands -- operand types are: const Fraction * int

I set the parameters to be const references because they are not written to... is this incorrect in this case or is there some way around this? Surely I don't have to define a whole load of duplications of the member functions as global free functions with two arguments just to add const to the parameters?

Toby
  • 9,696
  • 16
  • 68
  • 132
  • 2
    `Fraction Fraction::operator* (const Fraction &rightOperand) const ` – Richard Critten Feb 14 '18 at 22:27
  • `const` _after_ the parameter list?! Wow, that looks odd, don't think I would ever have figured that by myself XD – Toby Feb 14 '18 at 22:30
  • 2
    [C.168 from CppCoreGuidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c168-define-overloaded-operators-in-the-namespace-of-their-operands). You want to overload operators as (possibly `friend`) free functions most of the time – Justin Feb 14 '18 at 22:30
  • @Justin AFAICT the linked item says that overloaded operators should be defined in the same namespace as their operands... that can be the case for free or non-free functions though, no? – Toby Feb 14 '18 at 22:34
  • 1
    @Toby You may be right. I guess maybe there isn't a core guideline on that. At any rate, only a few of the operators should be overloaded as member functions. The idiom is for most binary operators to be overloaded as free functions. It helps in avoiding errors – Justin Feb 14 '18 at 22:36
  • 1
    @Toby: Which C++ book are you using? Function `const`ness is pretty fundamental. – Lightness Races in Orbit Feb 14 '18 at 22:37
  • 1
    @Toby one way of thinking about is that the binary operators create a new object, they don't modify an existing one, so there is no need for them to be member functions. – Richard Critten Feb 14 '18 at 22:40
  • Thanks @RichardCritten, very helpful info! It could make an answer? – Toby Feb 14 '18 at 22:41
  • @Toby: mmmmm yeah you're going to need to get a book Toby. I think it's clear that our free time is not supposed to be used as a substitute for you learning the language you have agreed to work in. – Lightness Races in Orbit Feb 14 '18 at 22:41
  • Good luck though - sounds like fun! – Lightness Races in Orbit Feb 14 '18 at 22:42
  • Would rather this was closed referencing the duplicate. – Richard Critten Feb 14 '18 at 22:42
  • @RichardCritten: While factually accurate on the surface, that logic would suggest that no `const` member function should exist, which is clearly false. As such I'm not sure that this is the best way to teach it – Lightness Races in Orbit Feb 14 '18 at 22:42
  • OK, , I'll close so :) – Toby Feb 14 '18 at 22:43
  • @LightnessRacesinOrbit was only rough guidance and I said "no need" not "must". – Richard Critten Feb 14 '18 at 22:43
  • @RichardCritten: was only a rough debunking :) – Lightness Races in Orbit Feb 14 '18 at 22:44
  • Seriously though granularity doesn't affect whether your students will heed your words - they will - so it's important not to misteach. I know you didn't mean to. You may still agree with your earlier comment even. YMMV :) – Lightness Races in Orbit Feb 14 '18 at 22:45

0 Answers0