青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

子彈 の VISIONS

NEVER back down ~~

C++博客 首頁 新隨筆 聯系 聚合 管理
  112 Posts :: 34 Stories :: 99 Comments :: 0 Trackbacks
C++ Tips
                  C++ Tips
                  Sections
            Intro
            I.   ABCs and Inheritance
            II.  Scope
            III. CLASSES
            IV.  MISC
            V.   OVERLOADING
            VI.  PARAMETERS
            VII. Constructors (more on classes)
            VIII.EXCEPTIONS
            IX.  TEMPLATES (more on classes)

Intro
This is not a code guideline document. See the C++ Style Guide for guidelines.
This is more of a document to fuel questions while you design and code
with C++. In some cases the point is simply stated and probably comes
off as a rule. In reality, they are simply meant as rules of thumb.
One of the main problems in C++ (more so than C) is that C++ provides
many mechanisms in the language by which the same task can be achieved
through different policies.  For example, C++ has the Polymorphism mechanism.
Some of the policies are templates, macros, inheritance, overloading,
(those are the static, compile-time ones), and virtual functions (the
run-time polymorphic policy).  Hopefully the following will provide 
enough fuel for questions to arrive at the best policy to use for a 
particular design.

I. ABCs and Inheritance
-----------------------
1. Abstract Base Class (ABC) : Make ABC class constructor protected when
      possible. Derived classes can have public constructor to override
      this.  The same is true for non-ABC classes as well.
2. Class Inheritance : Use protected keyword where ever possible, never use
      public to expose data members so the inheriting classes have access
      to them.
3. Multiple Inheritance and VBC's :The only drawback is the most derived
      class must initialize the lowest base classes. This breaks
      encapsulation.  Most people view multiple inheritance as a bad
      thing, but when used sparingly for parts of a design, there is
      no problem with it.
4. Data in classes should be keep private as much as possible.  Use a
      member function if a client needs to access the data.  If a member
      function does not need to be seen by a client, make it private.
      If the class might be inherited, then protected may be a good choice.
5. Private inheritance: Don't use it.  Too many ambiguities when used with
      run-time type identification.  Use a private data member instead
      or use public inheritance.
      Example:      class Foo: private Bar { ... }      dont do

6. Inheritance & virtual overrides: Care must be taken in overriding
      inherited functions. Sometimes functions are grouped together,
      and all need to be overridden. The base class designer must make
      this clear, if overriding one function requires multiple 
      functions to be overridden.
7. Inheritance & Get/Set functions: Typically functions that perform
      Get/Set shouldn't be overridden unless they are used by
      the derived class. Otherwise if the base class does direct
      field manipulation, you usually can't override it correctly,
      or it could be a maintenance nightmare.
      Note: Get/Set functions as referred to above are merely meant as
      abstractions. As a rule of thumb (thumb must be getting pretty
      big by now), one should not create functions called Get or Set,
      or flavors thereof. They tend to break the spirit of encapsulation.
      Of course, there will be times to use them.

II. SCOPE
---------
1. Law of Demeter : Do not make references to classes in a class that are
      not variables of the class, inherited by the class, or global.
      This also applies to including header files.
      Example:
            class Foo{public: Go(){} };
            class Bar{ Foo aFoo;  public:       Foo GetFoo(){return aFoo;} };
            class Fubar{ public:
                        void Bad(){  Bar aBar;  aBar.GetFoo().Go(); }
            };
      The method Bad() breaks encapsulation. It calls a method of a class
      it probably does not need to know about.  This will also affect 
      maintainability. If the Foo class changes, the changes may also 
      need to be done in the Fubar class.  
      The other side of the coin that must be looked at is do you
      want a pass-through method in the Bar class that simply
      calls the Go() method of the Foo class.  Lots of silly simple
      1 line member functions may not be desirable in all the classes.
      What probably needs to be looked at to decide what road to take
      is speed, or perhaps redesign the classes.
      Beyond the Law of Demeter is Doug's rule of thumb: Don't
      play hide and seek with data.
2. Scope: Another way to say the previous point is to keep scope
      small. This will increase the lifetime of the code and keep it
      maintainable and safe.
III. CLASSES
------------
1. Be explicit about the keywords, public, protected, and private in a class
      interface. Try not to have multiple sections. In other words, 
      multiple private sections in the class interface.  It is generally
      a good idea to place the public section first because this is 
      what most people are looking for when they go to use a class.
2. Make classes as atomic as possible. Give them a clear purpose. Break
      them into smaller classes if they become too complex. This may
      also eliminate duplicate code.
3. Don't let the compiler generate the default constructor,destructor,
      operator= and copy constructor.  If the class is entirely value
      based, this is probably fine, if not, for example, if the class
      has a data member that is a pointer, the above will probably not
      work.  Note, the default copy constructor only does a memcpy
      of the class, so all you copy is the pointer data. This may not
      be sufficient for copying a class.  Regardless, if you do want the
      default ones, place them in the code, and leave them commented.
      For example:
            // Fubar(const Fubar&)   use default copy constructor
4. If your class contains pointers, you should create the default constructor
      destructor, operator=, and copy constructor.
5. Class Copy: If the class should never be copied, then place the copy
      constructor in the private section and don't implement it.
      The linker will catch this, and the program will fail to build.
      This, although not graceful, is better than a malformed program.
6. Initialization: Perform all data member initialization in the constructor.
      It's best not to leave uninitialized objects running around in 
      the system.  Note, it is often more efficient using the
      constructor initializer list, otherwise, the default constructor
      would be called, and then you probably call member functions of
      the object later in the constructor.  For example:
      class Foo{ Bar mung;  Foo(int iCount) : mung(iCount) {}  ... };
      The variable mung is initialized once. But in the following:
      class Foo{ Bar mung;  Foo(int iCount){ mung = iCount; }  ... };
      mung's default constructor is called before the body of the
      class Foo's constructor is entered. Then mung is set again -
      this assumes that mung has an assignment operator. The net
      effect is that mung is initialized twice.
7. Class Naming:  There exists several ways to name classes that seem to
      work well for certain groups or people.  There is Hungarian notation
      and the "Taligent's Guide to Designing Programs" that document some
      of the more typical methods.

IV. MISC
--------
1. Implicit int:  The 'implicit int' rule will go away in the next 
      C++ standard. So for a proto like:  'main()'  you will have to 
      say 'int main()' in the future. Same for variables.
2. Preprocessor: Avoid it.  Use const values in the class, or inline
      functions instead of macros.  This is not to say, never use any
      #defines.
      Main reason:      #define MIN(a,b)      ( (aSomeFunction(); }

V. OVERLOADING
--------------
1. operator overloading:  It's syntactic sugar. Don't add them if they
      are not needed.  This does NOT refer to the typical ones like
      '=', '==', but ones like '()', '[]', '+'.  It does not always 
      make sense to add two objects together.

2. Overloading:  If a member function is conditionally executing code, 
      it may be a candidate for operator overloading, or just overloading.
3. Operator overloading and chaining: When designing an overloaded
      operator, think about whether it needs to be chained. For example:
            String cstr = "a" + "b" + "c";
      The String class's operator returns a reference to the String class.
      A partial implementation might be:
      
      class String{ public:
      String& operator+(const char *pcBuf){ 
                  // code to add the char* to the string
                  return *this;
      }
            ...
      };

VI. PARAMETERS
--------------
1. Argument Passing:  The first choice is typically a const ref.  The 
      const ref is basically an alias, and is easier to use than a 
      pointer.  It creates the same amount of instruction code as passing
      a pointer (for most cases). It's typically better than passing
      by value, where an object constructor will be called (if its an
      object).  As a rule of thumb, you might want too give the following
      a whirl:
            IN      const &
            OUT      &            If the object has the support functs
            INOUT      *&            Acts like a **.
      So for an IN parameter, what the 'const &' says, is here is a 
      reference to it, but you cannot modify the object. But you can
      call member functions that do not change the object ( member
      functions defined as const).  The OUT parameter is a parameter
      that is passed to a function that will modify it.  If the parameter
      needs to be created, then the INOUT parameter of *& may be a
      good choice.
2. Returning Ref:  In functions that return a reference, remember not to
      reference a temporary object and return it.  For example:
            String &Zippo(void){ ....  return String();
      What happens, is that the String() is a temporary object that
      upon return goes out of scope and is destroyed.  Thus, you
      return a reference to a destroyed object. Unfortunately, the
      program will probably work in most cases till it's shipped.
      The ol' Heisenbug!
3. Ref vs Pointer: Here's another way to look at when to use references,
      and when should to use pointers.
      C programmers sometimes don't like references since the
      reference semantics they provide isn't *explicit* in the caller's
      code. After a bit of C++ experience, however, one quickly realizes
      this "information hiding" is an asset rather than a liability. In
      particular, reuse-centered OOP tends to migrate the level of
      abstraction away from the language of the machine toward the
      language of the problem.  References are usually preferred over
      ptrs whenever you don't need "re-seating". This usually means that
      references are most useful in a class' public interface. References
      then typically appear on the skin of an object, and pointers on the
      inside. The exception to the above is where a function's parameter
      or return value needs a "sentinel" reference. This is usually best
      done by returning/taking a pointer, and giving the nil ptr (0) this
      special significance (references should always alias *objects*, not
      a dereferenced nil ptr).

VII. Constructors (more on classes)
-----------------------------------
1. Creating Constructors: These creatures should be simple. Try not to
      do anything in them that may generate errors.  Remember they
      don't have return values. If it's necessary to allocate
      memory or other complex things in the constructor, throw an exception
      if possible as the first recourse. Else, the class will have to be
      protected everywhere that may use something that may be in error.
      It's generally not a pretty sight to see an error returned in the
      constructor's signature.
2. Constructors: Another reason to keep them simple is in the case
      of inheritance.
3. Destructors:  It's responsibility is to release resources allocated 
      during the class's lifetime, not just from construction.
4. Member Initialization List: The constructor can have a comma separated
      member initialization list. If a class contains value based classes
      as data members, they can be initialized in the constructor. For
      example:
      class Foo{
            String cstr1;      // value based class called String
            String cstr2;      // value based class called String
      public:
            Foo(const char* pcStr1, const char*pcStr2):
                  cstr1(pcStr1), cstr2(pcStr2){}
      };
      With the variables cstr1, and cstr2 initialized in the constructor,
      they are initialized only once. Else, if they were initialized in 
      the body of the constructor, they would first be initialized with
      a default constructor, then again in the body.
      
VIII. EXCEPTIONS
----------------
1. Exceptions: Use exception hierarchies, possibly even derived from the
      Standard C++ ones.
2. Exceptions: Throw exceptions by value and catch them by reference.
      This way the exception handling mechanism cleans up anything
      created on the heap. If you throw exceptions by pointer, the
      catcher must know how to destroy them.  This is probably not
      a good coupling.  Even so, any up casting may slice and dice the
      object.

IX. TEMPLATES (more on classes)
-------------------------------
1. Templates:  Before creating new ones, see if they are in RogueWave,
      or part of the C++ Standard.
2. Templates:  When creating them, try to filter out any code that
      does not depend on the type, and place that into a base class.
      Thus, the template class itself is only the necessary information
      that depends on type. Good examples can be found in RogueWave.


Douglas J. Waters
(best reached on the internet)
Internet: waters@openmarket.com
Phone: (781) 359-7220
posted on 2008-07-18 09:17 子彈のVISIONS 閱讀(566) 評論(0)  編輯 收藏 引用 所屬分類: 1.x 臨時目錄
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲人成啪啪网站| 欧美日韩dvd在线观看| 午夜国产精品影院在线观看| 欧美日韩你懂的| 久久精品av麻豆的观看方式| 欧美在线亚洲一区| 亚洲高清免费视频| 亚洲欧美经典视频| 亚洲国产成人精品久久| 亚洲欧美激情诱惑| 亚洲国产视频直播| 亚洲精品在线三区| 在线日韩欧美| 久久久av水蜜桃| 久久精品人人做人人爽| 欧美午夜电影在线| 亚洲免费高清| 日韩一级精品| 欧美成人a视频| 在线亚洲免费| 国外成人免费视频| 亚洲专区免费| 欧美怡红院视频| 韩国一区二区在线观看| 免费在线播放第一区高清av| 蜜桃av久久久亚洲精品| 亚洲三级视频| 欧美粗暴jizz性欧美20| 日韩午夜视频在线观看| 亚洲字幕在线观看| 狠狠做深爱婷婷久久综合一区| 欧美在线91| 亚洲日本黄色| 欧美一区二区三区视频免费播放 | 国产一区 二区 三区一级| 久久天天躁狠狠躁夜夜爽蜜月 | 国产精品久久久久久久9999| 亚洲欧美日韩精品久久久| 欧美成人午夜剧场免费观看| 亚洲美洲欧洲综合国产一区| 国产精品视频在线观看| 久久资源在线| 欧美一区二区三区免费看| 欧美国产欧美亚洲国产日韩mv天天看完整| 宅男精品视频| 亚洲精品乱码久久久久久按摩观| 欧美美女福利视频| 麻豆av一区二区三区久久| 亚洲美女av黄| 亚洲电影一级黄| 亚洲欧美另类久久久精品2019| 在线观看不卡| 亚洲电影观看| 国产日韩欧美精品在线| 欧美图区在线视频| 欧美大片一区二区| 欧美高清视频一区二区三区在线观看| 亚洲一区三区视频在线观看| 亚洲视频999| 亚洲一区日本| 午夜在线a亚洲v天堂网2018| 亚洲在线视频| 欧美在线在线| 久久精品日产第一区二区| 久久99在线观看| 性欧美video另类hd性玩具| 亚洲欧美国产毛片在线| 一本色道久久综合精品竹菊| 999亚洲国产精| 欧美一级黄色网| 免费av成人在线| 国产日韩一区在线| 亚洲精品乱码久久久久| 亚洲一区精品电影| 久久亚洲欧美国产精品乐播| 亚洲人成网站影音先锋播放| 亚洲欧美国产一区二区三区| 欧美一区二区在线免费观看| 国产一区二区三区高清| 91久久精品日日躁夜夜躁国产| 91久久午夜| 久久久人成影片一区二区三区观看| 欧美刺激性大交免费视频| 亚洲免费综合| 欧美视频日韩视频在线观看| 亚洲韩国一区二区三区| 两个人的视频www国产精品| 亚洲桃花岛网站| 欧美日韩中文字幕精品| 亚洲国产欧美久久| 欧美肥婆bbw| 免费观看亚洲视频大全| 国内精品久久久久影院薰衣草| 午夜精品亚洲| 一区二区欧美亚洲| 国产精品va在线播放| 亚洲视频精选在线| 国产专区精品视频| 欧美激情精品久久久久久久变态 | 韩国av一区二区三区| 久久亚洲电影| 久久亚洲春色中文字幕| 宅男66日本亚洲欧美视频 | 韩国成人福利片在线播放| 久久精品亚洲| 欧美激情一区在线观看| 欧美亚洲视频在线观看| 欧美亚洲在线| 亚洲人成在线观看一区二区| 亚洲精品乱码久久久久久日本蜜臀 | 性欧美暴力猛交另类hd| 亚洲一区二区三区久久| 极品av少妇一区二区| 一区二区三区毛片| 国产亚洲欧洲997久久综合| 欧美a级片一区| 国产欧美精品一区二区色综合| 午夜精品久久久久影视 | 亚洲图片在线| 久久永久免费| 亚洲免费影视第一页| 亚洲欧美日韩人成在线播放| 黄色亚洲在线| 中文精品视频一区二区在线观看| 在线精品视频免费观看| 亚洲国产一二三| 亚洲国产精品久久久久久女王| 在线视频免费在线观看一区二区| 尤物在线精品| 午夜久久tv| 亚洲欧美国产精品va在线观看 | 久久欧美中文字幕| 国产精品va在线播放| 日韩亚洲视频| 亚洲免费播放| 欧美韩日精品| 亚洲国产日韩欧美综合久久 | 中文一区二区| 午夜精品亚洲| 在线亚洲+欧美+日本专区| 久久亚洲一区二区| 欧美暴力喷水在线| 亚洲第一网站免费视频| 老司机成人网| 亚洲国产毛片完整版| 一本到12不卡视频在线dvd| 国产精品专区h在线观看| 欧美一级久久久| 麻豆久久婷婷| 亚洲在线观看视频网站| 国内精品伊人久久久久av一坑| 久久久久久久久久久久久女国产乱 | 欧美日韩三级视频| 一区二区三区**美女毛片| 午夜精彩国产免费不卡不顿大片| 国产亚洲欧美日韩日本| 欧美成人tv| 在线亚洲美日韩| 亚洲人成免费| 欧美一区二区精品久久911| 亚洲黄页视频免费观看| 欧美日韩中国免费专区在线看| 性欧美大战久久久久久久免费观看| 欧美成人精品h版在线观看| 亚洲一区二区伦理| 亚洲精品国产精品乱码不99按摩| 国产日韩精品在线| 国产精品嫩草影院av蜜臀| 久久午夜电影| 91久久国产综合久久| 国产亚洲免费的视频看| 欧美国产日韩免费| 欧美精品日日鲁夜夜添| 在线观看一区| 国产伦精品一区二区三区高清| 欧美精品激情| 久久久久久电影| 免费人成精品欧美精品| 欧美亚洲一区二区三区| 亚洲无亚洲人成网站77777| 亚洲尤物视频在线| 欧美一级久久久久久久大片| 欧美在线视频一区| 久久久亚洲高清| 欧美色精品在线视频| 欧美大片一区| 国产精品久久激情| 在线免费观看成人网| 亚洲国产天堂久久综合| 99热在线精品观看| 久久都是精品| 久久久www成人免费毛片麻豆 | 久久精品视频在线| 嫩模写真一区二区三区三州| 欧美日韩国产精品| 亚洲欧洲精品成人久久奇米网| 日韩午夜免费| 欧美专区亚洲专区| 亚洲精品影院| 久久av免费一区|