What if we want a module to provide a global variable
So,
What is linking?
- The linker combines compiled object files into a single executable - in order to do so it not only combines the files but resolved external symbols
- Symbols that are declared not defined in a .cc file must be resolved by the linker
- e.g. In our
vec
file, we declared an overload addition operator between twovec
’s.
- e.g. In our
And yet, main.cc
- Called this function, and was compiled, but it only knows about the function declaration
- That’s because the compiler sees this function has been declared, generates place holder info in the object file to say it wants to call it, the linker then finds it in the other object file and updates the location of it in the compiler’s placeholder code
Suppose we wanted to create a linear algebra module.
This doesn’t compile, main.cc
and linalg.cc
both include vec.h
and linalg.h
, but linalg.h
includes vec.h
. So, these two .cc
files get two copies of. vec.h
, and thus two definitions of struct vec
. We cannot define something twice, so this is an error.
So, how can we fix this? One file is to remove vec.h
from linalg.cc
, but this is not good, since sometimes, it is impossible to tell what each include
has.
Solution: Header guard **\#include guard**
- The first time
vec.h
is included, the symbolVEC_H
is not defined, so the file (including the definition of VEC_H) is included. - Subsequent includes
VEC.H
is defied, so the\#ifndef
is false, and the file is suppressed.
- *Always: out
\#include
guards in .h files
DON’T: Put **using namespace**
in **.h files**
. It forces files that include your header to have that using directove
- *NEVER EVER include .cc files **
NEVER EVER compile .h files, their code gets compiled as part of the files that they are included in
Classes
- Class: Can put functions inside of
structs
. - Can make functions that can only be called on an existing piece of data
- Object is an instant of a class.
Function inside of a class is called a member function, or method
Some **OOP ** terminology
- A class is essentially a structure type that can potentially contain functions
Student
above, is a class
- An object is an instance of a class
s
above, is an object, instance of classStudent
- The functions are called
member functions
ormethods
- Likewise, the variables in a class are called
member variables
orfields
members
just refers to both the functions and variables::
is called the scope resolution operatorc::f
means f in the context ofc
(class or namespace)
, where::
- LHS is a
Class (or nampespace)
rather than an object
- LHS is a
- *So, ** inside the function
grade()
, what do the assignments, mt, and final mean? - Since these fields don’t exist until an object is created/instantiated, they refer to the fields of the object that the method is called on.
e.g.
Formally, methods take a hidden extra parameter which is called this
, which is a pointer to the object the method wad called on. In above, this == &billy
. So every member function takes this parameter.
The above is the same as the following:
Analagous to what we wrote before, the this->
was implicit. Only need to specify this->field
if there’s another variable in scope with that name.
- *Initializing Objects **
Better solution:
- Include a method that initializes called a
constructor
, needs the same name as theclass
.