| 173 | |
| 174 | ==== Avoid Virtual Inlines ==== |
| 175 | |
| 176 | Virtual functions will almost never gain any benefit from being inlined (unless callers cast the function itself, e.g.: `foo->Derived::bar()` instead of `foo->bar()`, which is a very uncommon practice). If a virtual function definition in a header file requires including an external header, consider moving the definition into the implementation file. E.g.: |
| 177 | |
| 178 | Bad: |
| 179 | {{{ |
| 180 | #!c++ |
| 181 | // MyClass.h |
| 182 | #include "YourClass.h" |
| 183 | class MyClass final : public BaseClass { |
| 184 | public: |
| 185 | void doFoo() final { m_yourClass->doFoo(); } |
| 186 | |
| 187 | private: |
| 188 | Ref<YourClass> m_yourClass; |
| 189 | }; |
| 190 | }}} |
| 191 | |
| 192 | Good: |
| 193 | {{{ |
| 194 | #!c++ |
| 195 | // MyClass.h |
| 196 | class MyClass final : public BaseClass { |
| 197 | public: |
| 198 | void doFoo() final; |
| 199 | |
| 200 | private: |
| 201 | Ref<YourClass> m_yourClass; |
| 202 | }; |
| 203 | |
| 204 | // MyClass.cpp |
| 205 | #include "MyClass.h" |
| 206 | #include "YourClass.h" |
| 207 | void MyClass::doFoo() { m_yourClass->doFoo(); } |
| 208 | }}} |