-3

My friends has some code about operator overloading. The code are as follows:

class Bar
{
public:
    int num;
    Bar(int n){
        num = n;
    }
};
class FooPa
{
// Consider it empty
};
class Foo : public FooPa
{
    Bar *b1, *b2;
    Bar operator + ( )// **I don't understand this function, how can I use it?**
    {
        int value=  b1->num +b2->num;
        Bar * b = new Bar(value);
        return (*b);
    }
};

I don't understand the function "Bar operator +()". He wants to mathematical add "b1.num" and "b2.num" and then return a new object. Does it make sense? How can I use this new operator "+", anyone can give an example?

fhlkm
  • 333
  • 1
  • 6
  • 14
  • 1
    The prototype for operator+ is wrong. You can't use it. This is not mentioning the memory leak and everything. – SergeyA Oct 06 '15 at 15:41
  • Down voting questions from inexperienced programmer is new fashion on SO. :P – anurag-jain Oct 06 '15 at 16:08
  • @SergeyA Its a unary + operator overload which is fine except the memory leak. – anurag-jain Oct 06 '15 at 16:10
  • @a4anurag: It's "fine"? What about reading from uninitialised pointers? Even if the implementation was correct, do you think a unary plus makes a custom type easier to use? – Christian Hackl Oct 06 '15 at 16:27
  • @ChristianHackl Those pointer initialisations is job of Foo's constructor's not overloaded + operator. Moreover it seems OP need clarification reg the function signature and less of its implementation. – anurag-jain Oct 06 '15 at 16:45
  • @a4anurag: I don't understand. There is no constructor in `Foo`. – Christian Hackl Oct 06 '15 at 16:46
  • @ChristianHackl Because OP has either not implemented it and relying on default ones or he's not posted it as part of code. – anurag-jain Oct 06 '15 at 16:47
  • @a4anurag: If he asks this question and does not post the required code, then we practically have to assume that he is not aware of the problem. – Christian Hackl Oct 06 '15 at 16:50

3 Answers3

3

The prototype Bar Foo ::operator +() means an unary plus operator. It can be used like this:

Foo f;
Bar b = +f; // "+f" is the unary plus operator
lisyarus
  • 15,025
  • 3
  • 43
  • 68
3

Normally Operator overloading means you are giving new definition to an existing operator. There are some exceptions and rules that needs to be followed(google it).

The basic syntax is

 <return type> operator "symbol" (parameters)

Eg: Lets say you have a class complex like this

class complex{
int real,img;
}

You need to overload the operator + in such a way that it can perform the addition of two complex numbers like this

public:
void operator+(complex ob)
{cout<<real+ob.real<<" + i "<<img+ob.img; }

Rules: The operator must be used along with a class object like this

complex a,b;
a+b;

Note: Here a is the calling object( ((a.)+b))This is taken care by the compiler). b has no connection with a apart from the fact that they are of the same datatype. So b is passed as a parameter(all binary operators must have an argument).

Coming to your example you have overloaded unary + operator . It is basically the same thing as binary overloading but you cannot pass arguments. Hope you understood the concept of operator overloading

Nirmal Raj
  • 729
  • 6
  • 18
2

I don't understand the function Bar operator +()"

Rightfully so. It does not make much sense (of course, with example names like "Foo" and "Bar", few things do). It declares a unary plus operator.

Which is already quite an exotic feature of the language. Here is an example:

#include <iostream>

int main()
{
    int i = 5;
    std::cout << +i << "\n";
}

This program will, unsurprisingly, print 5. There are some uses cases for it, though. See What is the purpose of unary plus operator on char array? and What does the unary plus operator do?.

In your code, you would call the operator like this:

Foo f;
+f;

The other strange thing about the code you show is that the operator returns Bar rather than Foo as one would expect (although, again, with names like "Foo" and "Bar", one cannot really be sure about the supposed intent of the code).

The operator is also private, which means that it can only be used from inside Foo's own functions or friends. This can hardly be right.

int value=  b1->num +b2->num;

This is undefined behaviour because you try to access members via uninitialised pointers[*]. Or you are not showing us the code where b1 and b2 are initialised and/or assigned.

Bar * b = new Bar(value);
return (*b);

This creates a memory leak, because the Bar object which lives in the memory dynamically allocated by new is copied and will never be destroyed, as the one and only pointer to it (b) is lost.


Perhaps your friend wants to create possibility of adding Bar and Foo together. If that is the case, then a lot of things have gone wrong. The function has the wrong interface and the wrong implementation.

Please go to http://en.cppreference.com/w/cpp/language/operators and read the section about binary arithmetic operators to see how this is done correctly.

Or just forget about operator overloading for now. Some more basic knowledge about the language (e.g. nullptr / pointer initialisation / pointers in general) seems to be missing.


[*] not nullptrs as a previous version of this answer said

Community
  • 1
  • 1
Christian Hackl
  • 27,051
  • 3
  • 32
  • 62