EDIT: I misread your question. This would be an answer to the question whether you can split header-files. It doesn't help you to avoid using LongClassName::
-syntaxes, sorry.
The simple answer: You can split up c++-file, but you can not split up header-files.
The reason is quite simple. Whenever your compiler needs to compile a constructor, it needs to know exactly how many memory it needs to allocate for such an object.
For example:
class Foo {
double bar; //8 bytes
int goo; //4 bytes
}
new Foo()
would require the allocation of 12 bytes
memory. But if you were allowed to extend your class definitions over multiple files, and hence split header files, you could easily make a mess of this. Your compiler would never know if you already told it everything about the class, or whether you did not. Different places in your code could have different definitions of your class, leading to either segmentation faults or cryptic compiler errors.
For example:
h1.h
:
class Foo {
double bar; // 8 bytes
int goo; // 4 bytes
}
h2.h
:
#include "h1.h"
class Foo {
double goo; // 8 bytes
} // we extend foo with a double.
foo1.cpp
:
#include "foo1.h"
Foo *makeFoo() {
return new Foo();
}
foo2.cpp
:
#include "foo2.h"
void cleanupFoo(Foo *foo) {
delete foo;
}
foo1.h
:
#include "h1.h"
Foo *makeFoo();
foo2.h
:
#include "h1.h"
#include "h2.h"
void cleanupFoo(Foo *foo)
main.cpp
:
#include foo1.h
#include foo2.h
void main() {
Foo *foo = makeFoo();
cleanupFoo(foo);
}
Carefully check what happens if you first compile main.cpp
to main.o, then foo1.cpp
to foo1.o
and foo2.cpp
to foo2.o
, and finally link all of them together. This should compile, but the makeFoo()
allocates something else then the cleanupFoo()
deallocated.
So there you have it, feel free to split .cpp
-files, but don't split up classes over header files.
& FooLongClassName::operator = (const U &) { /* code */ return *this; }` could become `template auto & operator = (const U &) { /* code */ return *this; }`.