http://www.eventhelix.com/realtimemantra/headerfileincludepatterns.htm
C++ Header File Include Patterns
Complex software in Realtime systems requires a careful header file management even when programming in C. When developers move to C++, header file management becomes even more complex and time consuming. Here we present a few header file inclusion patterns that will simplify this chore.
Header File Inclusion Rules
Here, we discuss the basic rules of C++ header file inclusion needed to simplify header file management.
- A header file should be included only when a forward declaration would not do the job.
- The header file should be so designed that the order of header file inclusion is not important.
- The header file inclusion mechanism should be tolerant to duplicate header file inclusions.
The following sections will explain these rules with the help of an example.
Header File Inclusion Example
The following example illustrates different types of dependencies. Assume a class A with code stored in a.cpp and a.h.
a.h
-
#ifndef _a_h_included_
-
#define _a_h_included_
-
#include "abase.h"
-
#include "b.h"
-
// Forward Declarations
-
class C;
-
class D;
-
class A : public ABase
-
{
-
B m_b;
-
C *m_c;
-
D *m_d;
-
-
public:
-
void SetC(C *c);
-
C *GetC() const;
-
-
void ModifyD(D *d);
-
};
- #endif
a.cpp
-
#include "a.h"
-
#include "d.h"
-
void A::SetC(C* c)
-
{
-
m_c = c;
-
}
-
C* A::GetC() const
-
{
-
return m_c;
-
}
-
void A::ModifyD(D* d)
-
{
-
d->SetX(0);
-
d->SetY(0);
-
m_d = d;
- }
File Inclusion Analysis
Lets analyze the header file inclusions, from the point of view of classes involved in this example, i.e. ABase, A, B, C and D.
Class ABase |
ABase is the base class, so the class declaration is required to complete the class declaration. The compiler needs to know the size of ABase to determine the total size of A. In this case abase.h should be included explicitly in a.h. |
Class B |
Class A contains Class B by value , so the class declaration is required to complete the class declaration. The compiler needs to know the size of B to determine the total size of A. In this case b.h should be included explicitly in a.h. |
Class C |
Class C is included only as a pointer reference. The size or actual content of C are not important to a.h or a.cpp. Thus only a forward declaration has been included in a.h. Notice that c.h has not been included in either a.h or a.cpp.
|
Class D |
Class D is just used as a pointer reference in a.h. Thus a forward declaration is sufficient. But a.cpp uses class D in substance so it explicitly includes d.h.
|
Key Points
The key points here are header files should be included only when a forward declaration will not do the job. By not including c.h and d.h other clients of class A never have to worry about c.h and d.h unless they use class C and D in substance.
Also note that a.h has been included as the first header file in a.cpp This will make sure that a.h does not expect a certain header files to be included before a.h. As a.h has been included as the first file, successful compilation of a.cpp will ensure that a.h does not expect any other header file to be includedbefore a.h. If this is followed for all classes, (i.e. x.cpp always includes x.h as the first header) there will be no dependency on header file inclusion.
Note that a.h includes the check on preprocessor definition of symbol _a_h_included_. This makes it tolerant to duplicate inclusions of a.h.
Cyclic Dependency
Cyclic dependency exists between class X and Y in the following example. This dependency is handled by using forward declarations.
x.h and y.h
-
/* ====== x.h ====== */
-
// Forward declaration of Y for cyclic dependency
-
class Y;
-
class X
-
{
-
Y *m_y;
-
...
-
};
-
/* ====== y.h ====== */
-
// Forward declaration of X for cyclic dependency
-
class X;
-
class Y
-
{
-
X *m_x;
-
...
- };
分享到:
相关推荐
Clean C++ Sustainable Software Development Patterns and Best Practices with C++ 17 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
### C++设计模式与衍生品定价 #### 一、引言 《C++设计模式与衍生品定价》是一本深入探讨如何在C++中利用设计模式实现金融模型的专业书籍。作者M.S. Joshi教授通过丰富的示例和实践案例,为读者提供了构建高质量、...
标题“C++ Designs and Patterns in Derivatives Pricing”指的是本书主要探讨如何在C++环境下利用设计模式来定价衍生金融产品。衍生金融产品(derivatives)是金融市场上的一种金融工具,其价值由基础资产(如股票...
Clean C++ Sustainable Software Development Patterns and Best Practices with C++ 17 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传...
"c++ Design-Patterns.rar"这个压缩包文件包含了全面的23种设计模式的实现,这对于C++开发者来说是一个宝贵的资源。以下是这23种设计模式的详细介绍,以及它们在实际编程中的应用。 1. **单例模式(Singleton)**:...
Design Patterns in Modern C++ Reusable Approaches for Object-Oriented Software Design 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书
The topic of Design Patterns sounds dry, academically constipated and, in all honesty, done to death in almost every programming language imaginable—including programming languages such as JavaScript...
Joshi - C++ Design Patterns and Derivatives Pricing - CUP[2008]2Ed.pdf》,是关于C++设计模式和衍生品定价的第二版教科书。本书着重讨论在C++中实现金融模型的背景下,设计模式这一面向对象编程的先进范式。...
### C++设计模式与衍生品定价 #### 一、引言 在当今金融工程领域,C++不仅是实现复杂数学模型的首选语言之一,也是构建高效、可维护金融软件的关键技术。《C++设计模式与衍生品定价》这本书通过具体实例介绍了如何...
Write maintainable, extensible, and durable software with modern C++. This book is a must for every developer, software architect, or team leader who is interested in good C++ code, and thus also ...
Apply design patterns to modern C++ programming Use creational patterns of builder, factories, prototype and singleton Implement structural patterns such as adapter, bridge, decorator, facade and ...
《大话设计模式》C++实现-design-patterns-cpp
Modern C++ Design: Generic Programming and Design Patterns Applied By Andrei Alexandrescu Publisher : Addison Wesley Pub Date : February 01, 2001 ISBN : 0-201-70431-5 ...
设计模式, 英文版, C++源代码 Design Patterns: Elements of Reusable Object-Oriented Software
Displaying extraordinary creativity and programming virtuosity, Alexandrescu offers a cutting-edge approach to design that unites design patterns, generic programming, and C++, enabling programmers ...