Putting Implementation in .h file

I have an assignment where I need to put the implementation in the header file instead of the .cpp file. I know it's not normal to do so, so I would like to know if there are any examples on how to do so? How does it look different and is there anything I have to watch out for?
Well, this is most of what you need to know:

myheader.h
1
2
3
4
5
6
7
inline int header_function_1() {
    return 0;
}
template <int I> int header_function_2() {
    return I;
};
extern int N;


myheader.cpp
int N=5;

main.cpp
1
2
3
4
5
6
7
8
9
#include <iostream>
#include "myheader.h"

int main()
{
    std::cout << inline_function_1() << ", " << inline_function_2<3>() <<
        ", " << N << std::endl;
    return 0;
}


Basically, to put code in headers you must have either an inline function, or a template-based function (or class function member).
For variables, the header must contain an "extern" keyword before, and the source must contain an "extern"-less version of the variable, initialized on place.
Last edited on
Topic archived. No new replies allowed.