Most of my classes have inlined methods : Get/Set, operators, simple constructors, etc. For the sake of readability I'm in the habit of separating their implementation from the declaration of the class, by putting them in a .inl file (the MFC way). The .inl file is included at the end of the .h file, so apart from the aesthetic consideration it is exactly the same thing as coding the functions' bodies in the class itself : if a single inlined function is modified, every .cpp file including this class directly or not is going to be recompiled, which can take some time in a big project.
//--------------------------------------------------------------------------------------------------------------------// // VectMat.h file #ifndef _VECTMAT_H_ #define _VECTMAT_H_ class CVect2D { public: // constructors inline CVect2D (void); inline CVect2D (const float fD); // x=y=fD inline CVect2D (const float fX, const float fY); // x=fX; y=fY [... etc ...] }; #include "VectMat.inl" #endif // _VECTMAT_H_ //--------------------------------------------------------------------------------------------------------------------// // VectMat.inl file #ifndef _VECTMAT_INL_ #define _VECTMAT_INL_ inline CVect2D::CVect2D() // do nothing {} inline CVect2D::CVect2D(const float fD) // x=y=fD { m_fV[_X_] = m_fV[_Y_] = fD; } inline CVect2D::CVect2D(const float fX,const float fY) // x=fX; y=fY { m_fV[_X_] = fX; m_fV[_Y_] = fY; } [... etc ...] #endif // _VECTMAT_INL_ //--------------------------------------------------------------------------------------------------------------------// // VectMat.cpp file #include "stdafx.h" #include "VectMat.h" [... implementation of the methods that are not inlined ...] |
//--------------------------------------------------------------------------------------------------------------------// // VectMat.h file #ifndef _VECTMAT_H_ #define _VECTMAT_H_ #ifdef _DEBUG #define INLINE #else #define INLINE inline #endif class CVect2D { public: // constructors INLINE CVect2D (void); INLINE CVect2D (const float fD); // x=y=fD INLINE CVect2D (const float fX, const float fY); // x=fX; y=fY [... etc ...] }; #ifndef _DEBUG #include "VectMat.inl" #endif #endif // _VECTMAT_H_ //--------------------------------------------------------------------------------------------------------------------// // VectMat.inl file #ifndef _VECTMAT_INL_ #define _VECTMAT_INL_ INLINE CVect2D::CVect2D() // do nothing {} INLINE CVect2D::CVect2D(const float fD) // x=y=fD { m_fV[_X_] = m_fV[_Y_] = fD; } INLINE CVect2D::CVect2D(const float fX,const float fY) // x=fX; y=fY { m_fV[_X_] = fX; m_fV[_Y_] = fY; } [... etc ...] #endif // _VECTMAT_INL_ //--------------------------------------------------------------------------------------------------------------------// // VectMat.cpp file #include "stdafx.h" #include "VectMat.h" |