0

I have read that where it is possible it is better to declare a class rather than include it (in a header file).

For example:

Class A;

Class B
{
public:
    B();
    ~B();

    A*   GetMyVariable();
    void SetMyVariable(A* newVal);

private:
    A* m_myVariable;
}

Which compiles fine and can be used no problem with the class definitions in their subsequent .cpp files.

However, supposing you now wish to make the get and set functioned inlined (or at least suggest to the compiler that they be) then code would need to change as follows:

//Class A;
#include "A.h"

Class B
{
public:
    B();
    ~B();

    inline A*   GetMyVariable()          { return m_myVariable; }
    inline void SetMyVariable(A* newVal) { m_myVariable = newVal; }

private:
    A* m_myVariable;
}

So the question is, is one method better than the other and why? Would I be better trying to get as much as possible using class declaration by removing the inline functions from some of my classes or should I keep trying to inline where ever I can?

TPS
  • 2,067
  • 3
  • 23
  • 32
  • 2
    The forward declaration still suffices in the second version (you don't need a complete type to set or return a pointer to it), and the `inline`s are already implicit in that context. – chris Jul 25 '14 at 17:09
  • OK, what if the member variable was not a pointer? – TPS Jul 25 '14 at 17:18
  • 1
    It's impossible to decide what's better without context and constraints, but you might be interested in reading [what-are-the-inlining-rules-within-c-classes](http://stackoverflow.com/questions/10867247/what-are-the-inlining-rules-within-c-classes). If the member is neither a pointer nor a reference, then its size and therefore the full declaration of the type is needed. – wonko realtime Jul 25 '14 at 17:25
  • 1
    You can use a forward declaration for a pointer reference, because pointer to anything are all the same size. You will have to #include the class definition of A if you refer to any of its member variables or methods. – Logicrat Jul 25 '14 at 17:26

2 Answers2

2

The real answer is that it's going to depend on your project and your style, because there are advantages to either way. Forward declaration avoids complicated include relationships and is usually easier for classes to use each other, while including can save you code and time by putting implementation in the header. Another consideration is that it's often easier/required to put implementation for templated classes in the header, so includes will often be necessary there.

My advice is to pick a policy to use and stick with it, if possible. I personally use "always include, except when classes need to use each other and includes create a loop." Then when I see a forward declaration, I know the relationship those two classes have.

jonas25007
  • 231
  • 1
  • 3
1

The term "better" depends on your compiler and the style guidelines that you follow.

Code inside the class may be inlined by the compiler. That is, the compiler may paste the content of the method rather than generate the method and calls to the method.

The inline decision is up to the compiler. It may inline, it may create a function, it may optimize and not create the function.

So, the decision is now in the hands of the author. The author may have to follow some coding guidelines that banish inline code. Some authors prefer to inline small method content. Others may prefer to put everything in the source file to prevent the build process from massive recompile because the header file was changed.

Do what you think will:

  1. Help you develop robust and correct code.
  2. Make maintenance easier.
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154