I really don't understand Operator Overloading(OO)!!!

And I really do want to be able to master them!
Does anyone have any good methods/ways to simply understand the logic behind it?
I am a beginner C++ coder with a little knowledge of CoboL language. Could anyone give me any good suggestions about them rather than just looking through C++ foundation books?

Thanks.

Problem:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class foo 
{
    int a; 
    public:
    foo(int x) : a(x) {}
};

int main()
{
    int i = 1;
    int j = 2;
    foo x(1), y(2);
    i + j; //correct
    x + y; //incorrect, compiler does not know how to add foo objects
    i = 5; //correct;
    x = y; //incorect, compiler does not know how to assign foo objects
}
Operator overloading is a way to tell compiler how to perform certain operations on user defined classes.
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
class foo 
{
    int a; 
    public:
    foo(int x) : a(x) {}
    int get() const {return a;}

//member operator overload. Assigment operators should be members
    foo& operator=(const foo& other) 
    {
        a = other.a;
        return *this;
    }
};

//Non-member overload
foo operator+(const foo& lhs, const foo& rhs)
{
     return foo(lhs.get() + rhs.get());
}

int main()
{
    int i = 1;
    int j = 2;
    foo x(1), y(2);
    i + j; //correct
    x + y; //works now
    i = 5; //correct;
    x = y; //works now
}

Last edited on
You need to step back a bit; let's look at C.

In C, you can write code like this.
1
2
3
4
5
6
7
8
9
10
int main()
{
    int a, b, c_sq;

    a = 3;
    b = 4;
    c_sq = a*a + b*b;

    return 0;
}


Now, what if we wanted to create our own 1024bit integer type and use it similarly? Well in C, define a struct to hold your data structure, and you'd create a set of functions that look like:
1
2
3
4
5
6
void int1024_init(struct int1024**);
void int1024_release(struct int1024*);
void int1024_set_from_string(struct int1024*, const char*);
void int1024_set_from_double(struct int1024*, double);
void int1024_add(struct int1024* ret, struct int1024* a, struct int1024* b);
void int1024_mul(struct int1024* ret, struct int1024* a, struct int1024* b);
Then to use it, your code miight look like:
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
int main()
{
    struct int1024 *a, *b, *c_sq;

    int1024_init(&a);
    int1024_init(&b);
    int1024_init(&c_sq);

    int1024_set_from_string(a, "3");
    int1024_set_from_string(b, "4");

    {
        struct int1024 *a_sq, b_sq;
        int1024_init(&a_sq);
        int1024_init(&b_sq);

        int1024_mul(a_sq, a, a);
        int1024_mul(b_sq, b, b);

        int1024_add(c_sq, a_sq, b_sq);

        int1024_release(b_sq);
        int1024_release(a_sq);
    }

    int1024_release(c_sq);
    int1024_release(b);
    int1024_release(a);

    return 0;
}


C++ allows you to create user defined types that act like built in types. So you can create a new class of object, int1024. The C++ language guarantees known behaviour when these things are created, destroyed, copied and moved using constructors, destructor and assignment operators.

You can also define your own + and - operators. This is called operator overloading. There's a whole bunch of operators that can be overloaded.

Putting it al together, the C++ equvalent of that C program would be:
1
2
3
4
5
6
7
8
9
10
int main()
{
    int1024 a, b, c_sq;

    a = 3;
    b = 4;
    c_sq = a*a + b*b;

    return 0;
}


There's some detail here: http://en.cppreference.com/w/cpp/language/operators
Last edited on
There are multiple aspects.

First is function overloading. There can be more than one function with same name, if they take different parameters. C does not have function overloading. Convenient; you don't have to come up with zillion names (foo, foof, food, fool, fooll, fooc) and you don't have to change all function calls if you change the type of a variable.

Operators are functions, but with syntactic sugar. You could write operator+(a, b), but you probably prefer a+b. That can make the code look almost like math.

C++ has user-defined types; (classes and structs). They don't have operators by default. Only the user (who defines the type) knows how to add up two objects (or whether it makes no sense). However, when the operators have been created, the type can be used conveniently. Math may not be of interest to you, but how about input and output (<< and >>)?
Thank you very much for the replys guys, I really appreciate it.
I am starting to get the concept of it finally. Cheers.

Zoo.
Topic archived. No new replies allowed.