[Cg1] Re: CPP Header files and local instances of classes

Leonhard Ferdinand Staut s9ldstau at stud.uni-saarland.de
Thu Nov 1 17:54:32 CET 2018

Hi Julian,

> If we look at the header file, we see two types of method stubs (how  
> do you call that again properly?): The ones inside the scope of the  
> class Vector and the ones OUTSIDE of that scope.
> What's the difference between those?

the ones inside the class are called methods, the ones outside are
called functions. (the proper term is declaration, not stub).
The main differences are what variables they can access and what
syntax you use to call them.

Methods are part of the class they are defined in.
- They have access to all private fields of the class. (Everything in
vector is public, so that doesn't really matter)
- They have an implicit parameter called "this" which is a pointer to
the object the method is called with.
- To call a method you use the dot syntax, e.g.:
Vector a(1.0,2.0,3.0);
This creates a vector "a" and calls the "length" method on it. In this
case the implicit parameter "this" of the length method is a pointer
to "a".

Functions on the other hand are not part of any class. For that reason
they are sometimes also called free functions.
- They do not have access to private fields (again, everything is public
in the vector, so this part doesn't really matter)
- They do not have an implicit parameter
- To call a function you just pass in all the parameters or use the
syntactic sugar if it's one of the "operator" functions.
Vector a(1.0,2.0,3.0);
float f = 2.0;
f * a;
This creates a vector "a" and uses syntactic sugar to call the function
operator*(f, a). In this case there is no implicit parameter. Instead
the vector is passed explicitly.
Btw, this syntactic sugar also works for the operators that are
declared as methods.

> Also, the operators: Let's take a look at operator+(const Vector& b)  
> const, which is binary.

As mentioned before, operator+ has been declared inside the class.
Therefore it is a method and has an implicit "this" parameter, i.e. a
pointer to the current instance of the class. So technically this
method has two parameters, because you always call the method on a
vector instance and also pass in one additional vector, e.g. if you
have two vectors "a" and "b", then "a + b" is syntactic sugar for
"a.operator+(b)", which is supposed to add the vectors "a" and "b".

Why some of the operators have been defined as methods of the class
while some others are free functions is a question I don't know the
answer to, especially since all fields of the vector are public
anyway. This design decision does indeed seem a little arbitrary.

> But it has only one argument. When trying to access the variables of  
> the current class (to use the binary operator when only one argument  
> is passed in the method), I have to create a new local instance of  
> the class inside the implementation of the operator. Can that be  
> right? I think this is super weird, because I am returning a new  
> instance of the class! Should I do this differently or is this not a  
> problem?

When implementing it, you can just access the x, y, z fields of the
current vector instance. Whenever you write "x" in the method, this
really means "this->x", i.e. the "x" of the vector you called the
method with.

Also, note how the method operator+ is declared. It returns a Vector
by value, and the method itself is declared as const (meaning the
method doesn't modify the fields of the vectors you passed in).
So yes, in the end you just have to return a new Vector instance which
is the result of the addition of the vector referenced by the "this"
parameter and the vector "b". Hint: can be done by just calling the
Vector constructor with the appropriate values, and returning that.

Some additional advice:
Put the implementation of the method in a different file, e.g.
vector.cpp. And make sure that you remember to write
Vector Vector::operator+(const Vector& b) const {
   // insert code here
so the compiler knows that operator+ is a method for the Vector class.
The functions you don't prefix with "Vector::" because they're not
part of the class.

I hope this helps.


> _______________________________________________
> Cg1 mailing list
> Cg1 at cg.uni-saarland.de
> http://mail.cg.uni-saarland.de/mailman/listinfo/cg1

More information about the Cg1 mailing list