3

My homework assignment is telling me to declare a class inline, but I've only heard of declaring functions inline. What does an inline class declaration do, and how do you achieve it?

unexplored
  • 1,414
  • 3
  • 18
  • 37
i love stackoverflow
  • 1,555
  • 3
  • 12
  • 24
  • Could you post the entire question? Honestly "an inline class" could mean more than one thing. Like, a class declaration inside a class, or a class declaration inside a function (both of which can be done and have use cases). – StilesCrisis Mar 03 '12 at 19:02
  • @StilesCrisis "All of the expression class declarations must appear in a header file named expressions.hh. (You should declare all of the classes inline in this one header file, since that will make everybody's lives much easier.)" – i love stackoverflow Mar 03 '12 at 19:03
  • 1
    I think they are just saying that your classes should go in the header, and for convenience all the class methods should be declared inline in the class itself, instead of putting the class methods out-of-line or into a separate CPP. It's slightly ambiguous but that's how I'd interpret it. – StilesCrisis Mar 03 '12 at 19:05
  • So are you sure they didn't say "... _define_ all of the classes inline"? – Mr Lister Mar 03 '12 at 19:05
  • Where do they expect you to look up information? Do you have course notes, a text book, or notes you have taken in class? I don't remember being asked to do anything at college without being given access to necessary resources. Not that we were spoon fed, but we could find things out for ourselves, if we tried. – Peter Wood Mar 03 '12 at 20:09
  • @PeterWood we have PowerPoint slides, but they only mentioned inline once and it was a few weeks ago – i love stackoverflow Mar 03 '12 at 20:36
  • Also see [What's the c++ inline class?](https://stackoverflow.com/q/5388097/608639) – jww Jul 24 '19 at 03:09

2 Answers2

5

I think they are just saying that your classes should go in the header, and for convenience all the class methods should be declared inline in the class itself, instead of putting the class methods out-of-line or into a separate CPP.

It's slightly ambiguous but that's how I'd interpret it.

StilesCrisis
  • 15,972
  • 4
  • 39
  • 62
  • 3
    Exactly how I interpret it. But the "make everybody's lives much easier" in the comment above is not a rule of thumb. It might be more convenient for very small classes, but it doesn't hold for most cases. – Correa Mar 03 '12 at 19:10
2

Comparing functions and classes:

A function can have a declaration and a definition:

ReturnType FunctionName(Parameter1, Parameter2); // a function declaration

ReturnType FunctionName(Parameter1, Parameter2) // a function definition
{
    // do something
    return ReturnType;
}

The function declaration gives all the information anyone needs who wants to call the function.

The function definition allows the compiler to generate machine code so that when someone calls the function there will be instructions somewhere in the executable that will be called.

This generated code exists only once in the executable. Usually you would put the function definition in a .cpp file (or .c if you're using C, this applies there too), which is compiled and linked into the executable. If that .cpp file isn't linked you will get a linker error saying FunctionName not found or something like that.

If you put the function definition in a header file it is more than likely that the header will be used in multiple places in your system, and included into other .cpp files. This will produce the opposite problem at link time FunctionName already defined in this, that, or the other object file.

So, functions usually aren't defined in header files because the header is usually included in multiple .cpp files so would end up producing duplicated object code.

With classes however it's slightly different. You have two different ways of defining a function for a class. One way is similar to how functions are defined above:

//myleanklass.h
class MyLeanKlass
{
public:
    void perform();
};

//myleanklass.cpp
void MyLeanKlass::perform()
{
    // Sing, dance, and play the piano
}

Just as with a free function we could have put the definition in the header file, as long as it wasn't included in more than one .cpp file. Otherwise we'd get a similar linker error again void MyLeanKlass::perform already defined ... This scenario is not the norm though. You usually declare classes so that they can be reused through a system. So the header will be included in multiple places and lead to linker errors.

Just to be clear, we could have put the definition in the header file, but not inline. You wouldn't be able to include this header multiple times in your system:

class MyLeanKlass
{
public:
    void perform();
};

void MyLeanKlass::perform()
{
    // Sing, dance, and play the piano
}

Finally then, we get to the answer to the question. You can define class functions in the header file as long as they are within the class declaration. This is called inline definition. Even if the header is included in multiple .cpp files it won't lead to multiple definitions in compilation units and won't cause linker errors.

//myleanklass.h
class MyLeanKlass
{
public:
    void perform()
    {
        // Sing, dance, and play the piano
    }
};

However, there are problems with this approach:

  • Inlining class functions is likely to make compilation take longer as the compiler will still compile every repeated definition of class functions and only throw away the duplicates at link time.
  • Making changes to class functions causes a recompilation of any .cpp file which includes it, again leading to longer re-compilation times.
  • Sometimes it just won't be possible. If a class uses another class and that uses the other class, you're going to have a cyclic dependency and it won't be possible to define everything inline. You can solve that by just putting the problem functions into a separate .cpp file.
Peter Wood
  • 23,859
  • 5
  • 60
  • 99