Hello, I was wondering which are the differences between declaring and implementing a class solely in a header file, compared with normal approach in which you protype class in the header and implement in effective .cpp file.
To explain better what I'm talking about I mean differences between normal approach:
// File class.h
class MyClass
{
private:
//attributes
public:
void method1(...);
void method2(...);
...
}
//file class.cpp
#include class.h
void MyClass::method1(...)
{
//implementation
}
void MyClass::method2(...)
{
//implementation
}
and a just-header approach:
// File class.h
class MyClass
{
private:
//attributes
public:
void method1(...)
{
//implementation
}
void method2(...)
{
//implementation
}
...
}
I can get the main difference: in the second case the code is included in every other file that needs it generating more instances of the same implementations, so an implicit redundancy; while in the first case code is compiled by itself and then every call referred to object of MyClass
are linked to the implementation in class.cpp
.
But are there other differences? Is it more convenient to use an approach instead of another depending on the situation? I've also read somewhere that defining the body of a method directly into a header file is an implicit request to the compiler to inline that method, is it true?