zoukankan      html  css  js  c++  java
  • [Js-C++].h文件与#include详解

    *****************************************************
    ** 4) The "right way" to include **
    *****************************************************


    Classes you create will often have dependencies on other classes. A derived class, for example, will always be dependent on its parent, because in order to be derived from the parent, it must be aware of its parent at compile time.

    There are two basic kinds of dependencies you need to be aware of:
    1) stuff that can be forward declared
    2) stuff that needs to be #included

    If, for example, class A uses class B, then class B is one of class A's dependencies. Whether it can be forward declared or needs to be included depends on how B is used within A:


    - do nothing if: A makes no references at all to B
    - do nothing if: The only reference to B is in a friend declaration
    - forward declare B if: A contains a B pointer or reference: B* myb;
    - forward declare B if: one or more functions has a B object/pointer/reference
    as a parementer, or as a return type: B MyFunction(B myb);
    - #include "b.h" if: B is a parent class of A
    - #include "b.h" if: A contains a B object: B myb;


    You want to do the least drastic option possible. Do nothing if you can, but if you can't, forward declare if you can. But if it's necessary, then #include the other header.

    Ideally, the dependencies for the class should be layed out in the header. Here is a typical outline of how a "right way" header might look:

    myclass.h

    //=================================
    // include guard
    #ifndef __MYCLASS_H_INCLUDED__
    #define __MYCLASS_H_INCLUDED__
    
    //=================================
    // forward declared dependencies
    class Foo;
    class Bar;
    
    //=================================
    // included dependencies
    #include <vector>
    #include "parent.h"
    
    //=================================
    // the actual class
    class MyClass : public Parent  // Parent object, so #include "parent.h"
    {
    public:
      std::vector<int> avector;    // vector object, so #include <vector>
      Foo* foo;                    // Foo pointer, so forward declare Foo
      void Func(Bar& bar);         // Bar reference, so forward declare Bar
    
      friend class MyFriend;       // friend declaration is not a dependency
                                   //   don't do anything about MyFriend
    };
    
    #endif // __MYCLASS_H_INCLUDED__ 

    This shows the two different kinds of dependencies and how to handle them. Because MyClass only uses a pointer to Foo and not a full Foo object, we can forward declare Foo, and don't need to #include "foo.h". You should always forward declare what you can -- don't #include unless it's necessary. Unnecessary #includes can lead to trouble.

    If you stick to this system, you will bulletproof yourself, and will minimize #include related hazards.

    mark一篇讲C++的 .h 和 #include 的好文章,贴一道链接慢慢看

    http://www.cplusplus.com/forum/articles/10627/

  • 相关阅读:
    阿里安全归零实验室招聘各路大牛!offer好说!
    露脸!钉钉通过SOC2隐私性原则审计,安全和隐私保护达超一流国际标准
    BAT齐聚阿里安全-ASRC生态大会:呼吁联合共建网络安全白色产业链
    v3-4_-vict-、-vinc-胜利,征服
    Grammar01 语法七要素之一_词类
    Grammar00_英语学习铁律
    SpokenEnglish01_ When's it due?
    SpringBoot31 重识Spring01-环境搭建、Actuator监控、属性配置、多环境配置
    Shrio04 自定义Realm
    Shrio03 Authenticator、配置多个Realm、SecurityManager认证策略
  • 原文地址:https://www.cnblogs.com/jiasq/p/8638399.html
Copyright © 2011-2022 走看看