Operators and linked lists confuse me, please help!

So, I'm taking classes in programming right now, and we've recently covered things called operators and linked lists. I have notes on them, a book, and a few examples (that unfortunately don't all seem to work), but I'm still confused about them.

In relation to operators, I'm not actually sure what they do, or how they're used. I've read up on it, but I'm still very confused as to how to use them or even how to build them.

In relation to linked lists, I think I understand how they work - each item in the list has an attached pointer that points to the next item, if I understand it correctly - but I always get stuck when I actually try to build one. I'm not really sure where to start, to be honest.


If anyone could help explain these to me, I would really appreciate it.
In Object Oriented Programming, the idea is to be as realistic as possible. In native programming languages, only types like integers, floating numbers, and boolean values are included in the default program. What if you would want to build your own type? let's say, a math vector?

In fact, for that, you start by creating a class, let's call it MathVector. This class should, however, have 3 elements for the 3 components in space. Let's call them x y z, and put them in the class too.

class MathVector
{
public: //leave this public part for later times. For now, just include it at the beginning of your class

double x;
double y;
double z;
}

Now we have a new type called MathVector. Whenever we define that type, we get an x, y and z!!! Let's use that in a small program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
    MathVector myFirstVector; //created a vector with 3 components
    MathVector mySecondVector; //created another vector with 3 components
    
    //now let's give the vectors some values

    myFirstVector.x = 5;
    myFirstVector.y = 3;
    myFirstVector.z = 7;

    mySecondVector.x = 2;
    mySecondVector.y = 1;
    mySecondVector.z = 6;

    //now we have 2 vectors. How about we add those 2 vectors and assign them to a third vector?

    MathVector myThirdVector;

    myThirdVector = myFirstVector + mySecondVector;
    //ooooops... this will not compile... why?
}


Read the code. Now this last line won't compile. Why? because you simply didn't tell your compiler what adding vectors mean. What the compiler sees is a container with 3 double values. It doesn't know what those values are. That's why you have to define the addition operator, operator+, to tell your compiler how it's supposed to add those vectors.

Let's edit that class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class MathVector
{
public:

    double x;
    double y;
    double z;

    friend MathVector operator+(const MathVector& left_hand_side, const MathVector& right_hand_side)
    {
          MathVector returnVal;
          returnVal.x = left_hand_side.x + right_hand_side.x;
          returnVal.y = left_hand_side.y + right_hand_side.y;
          returnVal.z = left_hand_side.z + right_hand_side.z;

          //here you told your compiler how addition has to be done. Now it would compile
          return returnVal;
    }
}


Now the main code with addition should compile. Because you informed your compiler that adding vectors mean adding each component to the corresponding one.

I'm not so sure whether the code is 100% syntactically correct. I just wanted to explain operators. So good luck implementing it :-)

Hope this helps!

Cheers.
So, what you did here is build a function with the word "operator+" attached to it, and this function does whatever you need the operator to do - assuming you build it right, of course. You can also do this for different types of operations then? For example, I'm guessing you could also build a multiplication operator in a similar manner (shouldn't be difficult at all), or even something more complicated, like a dot product operator or a probability finder.

And then all you have to do when you want to add the vectors (or whatever else you want to do) is simply implement the operator in the main code, passing in whatever you want to add/multiply/whatever into the operator function.
That's true, too many operations are supported. But you have to be careful not to overload some operators, like the . (dot) operator, because this operator is normally used for accessing class members. And the unary * operator (not the multiplication I mean, unary means used only from one side with no other variable, like i++), because this is used to dereference pointers of the class, and it would be so confusing if you overload it.

There are many tables on how to use different operators in C++ in different contexts. Google them and you'll find a lot of help. My example is just the start ;-).

Good luck!
Okay, this changes what the "+" sign actually does. So, in the example above, does the operator work as specified only in relation to class members, or is it a global change? I mean, in the example above, the operator now adds vectors together and produces another vector from it, but what if you wanted to add integers as well? Would it change the way int x + int y works, or would that remain unaffected since neither is a member of the class where the operator was declared?
Last edited on
It would not, since neither matches the parameter types of the overload defined here.
Okay, I think I got it. Thanks for all the help!
Topic archived. No new replies allowed.