What is the point of overloading operators in classes?

I have just started learning about overloading operators but i don't seem to understand their purpose.

Please correct me if I'm wrong, but i will try to explain what i have understood so far about overloading the "=" operator.

Overloading the "=" operator is just defining a member function whose purpose is to assign the values stored in the member variables of one class object to another...

Look at the code below to see what I have done so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
using namespace std;

class example1
{
    string name;
    int *pointer;
    int num_of_elements;

public:
    example1 ()
    {
        name = "";
        num_of_elements = 0;
    }

    example1 (string m, int num)
    {
        name = m;
        num_of_elements = num;
    }

    void overloadingOperator = (example1 & n)       //overloading =
    {
        cout << "The function has been called";
        name = n.name;
        num_of_elements = n.num_of_elements;
        pointer = new int [num_of_elements];

        for (int m = 0; m <= 4; m++)
            {
            pointer[m] = n.pointer[m];
            }
    }

};

int main ()
{
    example1 ex1 ("Abel", 5);

    example1 ex2;

    ex2 = ex1;

}




the compiler keeps insisting that the operator function is of type void and considers that an error, what does that mean?
Last edited on
On the example code:
I don't think you can add anything to the function name when overloading an operator. So on line 23, it should just be void operator =. And also, is int *pointer an array? If so, why is it only being initialized in the assignment operator class? Also, it is a good idea to use a copy constructor, destructor, and assignment operator if you need to use one. Finally, as I learned this week, the typical method implementation for assignment operator's is to use copy-and-swap, like described here: http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Copy-and-swap .
This question may also be relevant: http://www.cplusplus.com/forum/beginner/147366/

On the use, why:
You said it yourself, to assign values of one object to another. Normally, this is all handled for you by the default assignment operator. However, when dealing with pointers and memory on the heap, these aren't enough to get the job done most of the time. There will be ambiguities, bugs, and errors. That's why there are custom assignment operators, copy constructors, and destructors: to make sure each object gets its a copy for itself of data that it owns, and when they are done with the data, it goes away neatly.
Last edited on
Thank you for your response.

And yes, *pointer is a pointer to a dynamically allocated array and i completely forgot to initialize it in the constructors. I know that i could've use the swap function or a copy constructor, but I am simply experimenting with overloading operators to see what i can do with them.

However I am still having some trouble using a member function that overloads an operator. So when making a function that overloads an operator, do i even need to include a return type or any parameters?

Here is my updated code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
using namespace std;

class example1
{
    string name;
    int *pointer;
    int num_of_elements;

public:
    example1 ()
    {
        name = "";
        num_of_elements = 0;

    }

    example1 (string m, int num, int nums)
    {
        name = m;
        num_of_elements = num;
        pointer = new int [nums];
    }

    ~example1 ()
    {
        cout << "Freeing up space" << "\n";
    }


    void Operator = (example1 &n)     //should i remove the return type and the parameters? compiler keeps saying there's an error here
    {
        cout << "The function has been called";
        name = n.name;
        num_of_elements = n.num_of_elements;
        pointer = new int [num_of_elements];

        for (int m = 0; m <= 4; m++)
            {
            pointer[m] = n.pointer[m];
            }
    }

};

int main ()
{
    example1 ex1 ("Abel", 5, 5);

    example1 ex2;
    
    ex1 = ex2;      //calling operator member function

}


By the way, what is the difference between using a copy constructor and overloading the "=" operator in a member function?
Last edited on
The assignment operator should be declared
example1 &operator=(const example1 &n);
It should do nothing if you try to assign an object to itself. Also, aren't there are num_of_elements in the pointer array? So the code should be:
1
2
3
4
5
6
7
8
9
10
11
{
    if (&n != this) {
        num_of_elements = n.num_of_elements;
        pointer = new int [num_of_elements];
        for (int m = 0; m < num_of_elements; m++)
            {
            pointer[m] = n.pointer[m];
            }
    }
    return *this;
}


Last edited on
Ah thank you, I completely forgot about the operator keyword. I made some mistakes in the code since i was only focusing on the overloading operator function. Thanks again!
Topic archived. No new replies allowed.