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

子彈 の 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 閱讀(570) 評論(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>
            亚洲茄子视频| 亚洲国产精品一区二区www| 国产精品初高中精品久久| 欧美日韩亚洲精品内裤| 国产毛片一区二区| 亚洲成人在线网| 99热在这里有精品免费| 亚洲精品国产日韩| 久久久水蜜桃| 99精品欧美一区二区三区| 亚洲欧美日韩综合| 欧美高清视频在线| 国产一区二区三区四区hd| 一区二区高清视频在线观看| 久久精品人人爽| 国产精品99久久久久久久女警 | 亚洲天堂视频在线观看| 久久精品视频在线播放| 欧美一级久久久久久久大片| 欧美日韩一区二区三区| 香蕉久久国产| 99天天综合性| 国内外成人在线| 99日韩精品| 亚洲欧美三级伦理| 亚洲精品视频在线播放| 亚洲自拍电影| 欧美视频一区二区三区…| 亚洲免费观看高清在线观看| 欧美国产成人精品| 久久经典综合| 国产综合精品一区| 99精品欧美一区二区三区综合在线| 国产日韩1区| 久久国产欧美| 欧美另类女人| 99视频超级精品| 久久精品人人爽| 国模精品一区二区三区| 亚洲最新色图| 亚洲区在线播放| 久久精品99国产精品酒店日本| 亚洲最新在线| 美女黄色成人网| 亚洲国产日韩在线一区模特| 蜜桃精品一区二区三区 | 欧美大片免费久久精品三p| 欧美日韩一区二区国产| 欧美激情视频一区二区三区不卡| 男同欧美伦乱| 亚洲激精日韩激精欧美精品| 欧美亚洲日本网站| 午夜欧美不卡精品aaaaa| 亚洲欧美伊人| 亚洲专区在线| 久久国产99| 欧美在线国产| 欧美一区二区三区四区在线观看 | 亚洲午夜一区二区| 亚洲免费在线视频| 在线亚洲激情| 欧美日韩亚洲一区二区三区在线观看| 欧美激情国产高清| 亚洲国产精品成人综合| 亚洲伦理在线免费看| 国产精品免费看久久久香蕉| 久久久久久欧美| 国产一区二区三区丝袜 | 亚洲伦理精品| 一区二区三区波多野结衣在线观看| 蜜臀av性久久久久蜜臀aⅴ四虎| 老司机成人网| 欧美午夜精品久久久| 久久天天狠狠| 欧美日韩亚洲国产精品| 99精品99| 欧美在线日韩在线| 狠狠狠色丁香婷婷综合激情| 久久激情综合网| 欧美成人精品不卡视频在线观看 | 性做久久久久久久免费看| 亚洲国产成人av在线| 欧美1区3d| 一本色道久久综合狠狠躁篇怎么玩 | 亚洲欧美在线免费观看| 国产欧美在线视频| 久久青草久久| 久久精品99国产精品日本 | 欧美freesex交免费视频| 欧美激情国产精品| 亚洲伊人久久综合| 国产农村妇女毛片精品久久麻豆| 欧美有码在线视频| 一区二区三区欧美在线| 国产精品国产三级国产aⅴ浪潮| 亚洲特级片在线| 日韩视频在线观看一区二区| 欧美视频中文字幕| 久久经典综合| 日韩小视频在线观看专区| 亚洲日本成人女熟在线观看| 欧美日韩免费观看一区二区三区| 亚洲综合成人在线| 欧美激情精品久久久| 亚洲欧美激情在线视频| 欧美日韩在线播放一区二区| 香蕉成人啪国产精品视频综合网| 欧美黑人多人双交| 性做久久久久久久免费看| 亚洲福利免费| 国产日韩专区在线| 欧美一区二区视频97| 亚洲国产欧洲综合997久久| 午夜精品视频一区| 日韩网站在线| 激情久久久久久| 蜜臀91精品一区二区三区| 亚洲一区二区三区四区在线观看| 亚洲免费人成在线视频观看| 激情成人综合| 国产日韩精品在线观看| 欧美精品日日鲁夜夜添| 日韩一级片网址| 欧美国产亚洲精品久久久8v| 欧美一区日韩一区| 亚洲欧美区自拍先锋| 亚洲精品视频免费| 亚洲国产二区| 在线观看欧美| 欧美精品黄色| 欧美大片在线看| 亚洲视频在线二区| 亚洲精品日韩综合观看成人91| 免费欧美日韩| 久久综合伊人77777| 久久精品国产视频| 欧美一区1区三区3区公司| 亚洲一区三区电影在线观看| 99国产精品私拍| 一区二区成人精品| 在线午夜精品自拍| 亚洲性人人天天夜夜摸| 亚洲视频观看| 亚洲欧美激情视频| 午夜国产欧美理论在线播放| 亚洲视频在线视频| 亚洲免费视频中文字幕| 午夜精品视频在线| 久久精品亚洲乱码伦伦中文| 久久精品国产亚洲精品| 久久人人爽人人| 麻豆成人在线观看| 亚洲第一精品影视| 欧美呦呦网站| 久久久久国产一区二区三区| 日韩亚洲国产精品| 中文亚洲字幕| 欧美一区二区三区视频在线观看| 欧美在线1区| 蜜臀久久99精品久久久画质超高清| 麻豆精品在线播放| 亚洲国产欧美一区二区三区丁香婷| 亚洲国产精品嫩草影院| 99精品视频一区| 亚洲欧美在线aaa| 久久久久久久一区二区三区| 亚洲永久精品大片| 久久九九电影| 欧美激情欧美狂野欧美精品| 国产精品久久久久久久久久ktv| 国产日韩欧美二区| 亚洲国产精品成人| 亚洲欧美国产制服动漫| 久久综合图片| 在线亚洲成人| 久久综合色婷婷| 久久免费精品日本久久中文字幕| 欧美阿v一级看视频| 国产精品色网| 国产精品日韩欧美一区二区三区| 国产亚洲aⅴaaaaaa毛片| 亚洲国产一区二区三区a毛片| 亚洲一区二区网站| 欧美成熟视频| 午夜久久美女| 欧美日韩一区二区三区视频| 国内成+人亚洲| 亚洲视频一二区| 欧美高清你懂得| 午夜精品亚洲一区二区三区嫩草| 欧美岛国激情| 精品成人乱色一区二区| 亚洲欧美综合| 亚洲人成网在线播放| 亚洲欧洲综合另类| 亚洲三级视频在线观看| 欧美一区激情| 国产精品一区二区久久国产| 夜夜嗨av一区二区三区| 欧美成人一区在线|