Skip to Main Content
C++ Cookbook
book

C++ Cookbook

by D. Ryan Stephens, Christopher Diggins, Jonathan Turkanis, Jeff Cogswell
November 2005
Beginner to intermediate content levelBeginner to intermediate
594 pages
16h 23m
English
O'Reilly Media, Inc.
Content preview from C++ Cookbook

2.5. Including an Inline File

Problem

You have a number of membe r functions or standalone functions that you want to make inline, but you don’t want to define them all in the class definition (or even after it) in the header file. This way, you keep declaration and implementation separate.

Solution

Create an .inl file and #include it at the end of your header file. This is equivalent to putting the function definition at the end of the header file, but this lets you keep declaration and definition separate. Example 2-6 shows how.

Example 2-6. Using an inline file

// Value.h
#ifndef VALUE_H_  _
#define VALUE_H_  _

#include <string>

class Value {
public:
   Value (const std::string& val) : val_(val) {}
   std::string getVal() const;
private:
   std::string val_;
};

#include "Value.inl"

#endif VALUE_H_  _

// Value.inl
inline std::string Value::getVal() const {
   return(val_);
}

This solution doesn’t require much explanation. #include is replaced with the contents of its argument, so what happens here is that the contents of Value.inl are brought into the header file. Any file that includes this header, therefore, has the definition of the inline functions, but you don’t have to clutter up your class declaration.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C++ System Programming Cookbook

C++ System Programming Cookbook

Onorato Vaticone

Publisher Resources

ISBN: 0596007612Supplemental ContentErrata Page