1

Good Evening,

Im new to C++ but I was under the impression it was a no go to Define functions within header files.

I've been looking at some open source code and it seems a class has been defined within a header file and not split into .h / .cpp.

class Action
{
public:
    virtual ~Action() {}
    
    virtual string GetName() { return "Action"; }
    virtual void RequestUpdate(ActionContext* context) {}
    virtual void Do(ActionContext* context, double value) {}
    virtual void Touch(ActionContext* context, double value) {}
    virtual double GetCurrentNormalizedValue(ActionContext* context) { return 0.0; }
    virtual double GetCurrentDBValue(ActionContext* context) { return 0.0; }

    int GetPanMode(MediaTrack* track)
    {
        double pan1, pan2 = 0.0;
        int panMode = 0;
        DAW::GetTrackUIPan(track, &pan1, &pan2, &panMode);
        return panMode;
    }
};

There seem to be many other header files defining functions in the same project. Am I missing something here?

Full Header File HERE

  • 3
    There's nothing stating you _can't_ define a function in a header file, but it can increase compilation times. It's not uncommon for very short one-line or zero-line functions to be written in header files. It's also one of the easier ways to handle [template stuff](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) though this case isn't that. – Nathan Pierson Feb 12 '21 at 16:04
  • Another option I like to do when I want my header to be streamlined, I create a .inl file that I #include at the bottom of the header file. That .inl file has all of my inline methods explicitly declared `inline int MyClass::GetPropertyA() const {return m_PropertyA;}`, for example. – franji1 Feb 12 '21 at 16:28

1 Answers1

3

When is it acceptable to define a function in a header file?

When all of the following apply:

  1. The function is declared inline (whether explicitly or implicitly).
    • If you define a non-inline function header and include the header into more than one translation unit, you will violate ODR making the program ill-formed (no diagnostic required).
    • Note that when the function is declared inline, it has to be defined in all TU that use it and thus should be defined in a header. Thus, if other conditions below don't apply, the function shouldn't be declared inline.
    • Note that implicitly instantiated function templates are inline functions. Thus, unless you have fixed set of template instantiations, template functions practically have to be defined in headers. Same applies to non-template functions within class templates.
  2. The function doesn't introduce new dependencies to the header.
    • Dependencies of headers propagate as dependencies to anything that includes the header. This cause increases coupling between components which is undesirable.
  3. The function doesn't change often.
    • Headers tend to be included into multiple TU, and modifying a header consequently necessitates re-compilation of multiple TU which slows down development in large projects.
  4. The function is cheap to compile. (This doesn't matter if the function is in a pre-compiled header)
    • Headers tend to included into multiple TU, and repeating expensive compilation can significantly slow down compilation from scratch.
eerorika
  • 232,697
  • 12
  • 197
  • 326