In a project I’m working on, I have a fairly large templated class that I’ve implemented like this:
I have my header file
// MyBigClass.h
#ifndef MYBIGCLASS_H
#define MYBIGCLASS_H
template <typename T>
class MyBigClass {
/* -- snip -- */
};
#include "MyBigClass.cpp"
#include "MyBigClass_iterator.cpp"
#include "MyBigClass_complicatedFunctionality_1.cpp"
#include "MyBigClass_complicatedFunctionality_2.cpp"
#endif
And then all of my implementation files look basically like this:
// MyBigClass_foobar.cpp
template <typename T>
void MyBigClass<T>::member_1(){
/* -- snip -- */
}
template <typename T>
int MyBigClass<T>::member_2(int foo, T & bar){
/* -- snip -- */
}
// etc, etc
In main.cpp, I just include MyBigClass.h, and everything works and compiles fine. The reason I’ve split the implementation into many files is because I prefer working on three or four 200-400 line files, versus one 1200 line file. The files themselves are fairly logically organized, containing for example only the implementation of a nested class, or a group of interrelated member functions.
My question is, is this something that is done? I got a strange reaction when I showed this to someone the other day, so I wanted to know if this is a bad practice, or if there is a better, more usual way to accomplish something like this.
It’s convention to generally not include
cppfiles (there are limited and exotic cases when this is done), that’s probably the reason you got the weird looks.Usually this separation is done by moving the implementation to an
.implor even a.hfile instead of acppfile.And no, there’s nothing wrong with separating the implementation of templates and including the file in the header.