⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 csdn_文档中心_微软office的源代码样式规范 —— 绝对机密文档!!!.htm

📁 csdn10年中间经典帖子
💻 HTM
📖 第 1 页 / 共 3 页
字号:
            inline get and set member functions.  This will get optimized 
            into the same code as a public data member.  For 
            example:<BR>class Counter<BR>&nbsp;{<BR>public:<BR>&nbsp;int 
            CItems() const { return m_cItems; }<BR>&nbsp;void SetCItems(int 
            cItems) { m_cItems = cItems; }<BR>private:<BR>&nbsp;int 
            m_cItems;<BR>&nbsp;};</P>
            <P>Summary:<BR>&#61623; Data members use the naming convention m_name.<BR>&#61623; 
            Do not declare public data members.&nbsp; Use inline accessor 
            functions for performance.<BR>2.4 Virtual Functions<BR>Virtual 
            functions are used to allow derived classes to override a method in 
            a base class by providing their own implementation in a way that 
            always causes the most-derived version to be called whenever a 
            method is called through an object pointer, even if that pointer is 
            declared as a pointer to the base class.&nbsp; This is usually done 
            to implement polymorphism, and that抯 when we抣l use them.&nbsp; For 
            example, all COM interface methods are virtual because you are 
            always going for polymorphism via a standard interface.<BR>Unlike 
            simple member functions, virtual functions incur some overhead due 
            to need to call through the vtable.&nbsp; If a class contains at 
            least one virtual function then the data size of each instantiated 
            object will be 4 bytes larger than the combined size of the declared 
            data in order to hold the vtable pointer.&nbsp; After the first 
            virtual function, each additional one only adds another entry to the 
            class vtable, which is static and per-class (nothing per object), so 
            the main concern here is whether a class has any virtual functions 
            at all.&nbsp; In addition to the memory overhead, there is the 
            overhead to indirect a pointer twice before calling the 
            function.&nbsp; This is fairly fast and compact in 32-bit code, but 
            affects speed and size nevertheless. Perhaps the worst part is that 
            virtual functions cannot be inlined, so there will always be a 
            function call, even when the work is trivial.&nbsp; <BR>Because they 
            have overhead, you should not use virtual functions in a class 
            unless you need to.&nbsp; However, make sure you do use them when it 
            makes sense.&nbsp; In particular, if you have a base class which 
            requires a destructor, then the destructor should definitely be 
            virtual to allow derived classes to destruct any added members 
            properly.&nbsp; If the destructor were not virtual, then in a 
            context where polymorphism is being used (so the object pointer is 
            declared as a pointer to the base class), the base class destructor 
            will always get called, even for an object of a derived class that 
            added data members and declared its own destructor in an attempt to 
            free them.&nbsp; The derived class抯 destructor will only get called 
            if the base class destructor is declared virtual.&nbsp; This 
            scenario applies to many other kinds of methods that you will add to 
            your classes.&nbsp; In fact, most of the methods in a base class 
            might be this way if polymorphism is intended.&nbsp; This issues is 
            discussed in more detail in the Inheritance section below.<BR>Note 
            that although virtual functions have a performance penalty over 
            regular member functions, they are often the most efficient way to 
            implement a concept such as polymorphism where the alternative would 
            be large switch statements (not to mention the benefits of the 
            object-oriented encapsulation).<BR>Summary:<BR>&#61623; Use virtual 
            functions to implement polymorphism.<BR>&#61623; Virtual functions have 
            overhead, so don抰 use them unless you really should.<BR>&#61623; A 
            destructor in a base class should always be virtual if polymorphism 
            is intended.<BR>2.5 Constructors<BR>Ah, constructors.&nbsp; Every 
            new C++ programmer抯 nightmare.&nbsp; This is one reason to try to 
            minimize the use of constructors -- C programmers aren抰 used to them 
            and will get confused.&nbsp; Another reason is the infamous 
            performance overhead of calling a function (unless it抯 inline) and 
            doing work at possibly unexpected and/or redundant 
            times.<BR>However, using constructors can eliminate the dangers of 
            uninitialized data and can also made the code simpler to read (if 
            you抮e used to it).&nbsp; Judicious use of destructors (see below) 
            which match the constructors can also help prevent memory leaks and 
            other resource management problems.<BR>Fortunately, the issue is 
            mainly one when classes are declared on the stack or passed by 
            value, both of which we will avoid.&nbsp; Most of our classes should 
            be dynamic memory objects&nbsp; which will be passed around by 
            pointer.&nbsp; In this case, the constructor is essentially just a 
            helper function for the functions that create these dynamic 
            objects.&nbsp; Using a constructor for this purpose is reasonable to 
            ensure a clean and consistent initialization (if you make sure to 
            initialize all data members), but to prevent potential performance 
            problems due to redundant initialization the constructor should not 
            do anything expensive.&nbsp; Simply assigning a constant or a 
            parameter value to each data field is about right.&nbsp; Very simple 
            constructors can be made inline.&nbsp; <BR>Most importantly, a 
            constructor should never be able to fail, because lacking a fancy 
            exception handling mechanism, the caller has no way to handle this 
            in some cases.&nbsp; Any initialization that can fail (e.g. memory 
            allocations) should be put in a separate initialization member 
            function (called, e.g., FInit).&nbsp; When this is the case, it is 
            often useful to encapsulate the creation of an object in a function 
            (a global function or a member of another class) that calls new and 
            then FInit for the object, and returns the result of FInit.&nbsp; 
            For example:<BR>class Foo<BR>&nbsp;{<BR>public:<BR>&nbsp;Foo(int 
            cLines) { m_hwnd = NULL; m_cLines = cLines}<BR>&nbsp;virtual 
            ~Foo();<BR>&nbsp;BOOL FInit();<BR>&nbsp;void 
            DoSomething();<BR>private:<BR>&nbsp;HWND m_hwnd;<BR>&nbsp;int 
            m_cLines;<BR>&nbsp;};</P>
            <P>BOOL FCreateFoo(int cLines, Foo **ppfoo)<BR>{<BR>&nbsp;if 
            ((*ppfoo = new Foo(cLines)) == NULL)<BR>&nbsp;&nbsp;return 
            FALSE;<BR>&nbsp;if (*ppFoo-&gt;FInit())<BR>&nbsp;&nbsp;return 
            TRUE;<BR>&nbsp;delete *ppFoo;<BR>&nbsp;*ppFoo = 
            NULL;<BR>&nbsp;return FALSE;<BR>}</P>
            <P>BOOL Foo::FInit()<BR>{<BR>&nbsp;m_hwnd = 
            CreateWindow(...);<BR>&nbsp;return (m_hwnd != NULL);<BR>}</P>
            <P>Summary:<BR>&#61623; Do not do expensive work in a constructor.<BR>&#61623; If 
            you do make a constructor, make sure to initialize all data 
            members.<BR>&#61623; Very simple constructors can be made inline<BR>&#61623; A 
            constructor should never fail.&nbsp; Do memory allocations and other 
            potential failures in an FInit method.<BR>&#61623; Consider making a 
            creation function that encapsulates the new and FInit 
            operations.<BR>2.6 Destructors<BR>If a class has resources that need 
            to be freed, then the destructor is a convenient place to put 
            this.&nbsp; The normal case for us will be that this is just the 
            central place to free resources for an object that is freed via 
            delete (see below).&nbsp; The trickier use of destructors is for 
            stack-allocated classes, but we抮e going to avoid that by not using 
            classes on the stack.&nbsp; <BR>A destructor should be careful to 
            destroy an object properly regardless of how it was created or 
            used.&nbsp; Furthermore, if you choose to implement a method that 
            frees any resources before the actual destruction, make sure to 
            reset those fields (e.g. set pointers to NULL) so that a destructor 
            will not try to free them twice.&nbsp; It is not necessary for the 
            destructor to reset any fields, though, because the object cannot be 
            used after it is destructed.<BR>Like a constructor, a destructor can 
            never fail.&nbsp; Also, as stated above, a destructor in a base 
            class should always be declared virtual to make polymorphism 
            work.<BR>The destructor for the above example would be defined 
            as:<BR>Foo:~Foo()<BR>{<BR>&nbsp;if (m_hwnd != 
            NULL)<BR>&nbsp;&nbsp;DestroyWindow(m_hwnd);<BR>}</P>
            <P>Summary:<BR>&#61623; Use a destructor to centralize the resource cleanup 
            of a class which is freed via delete.<BR>&#61623; If resources are freed 
            before destruction, make sure the fields are reset (e.g. set 
            pointers to NULL) so that a destructor will not try to free them 
            again.<BR>&#61623; A destructor should never fail.<BR>&#61623; A destructor in a 
            base class should always be declared virtual if polymorphism might 
            be used.<BR></P><BR></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR>
<TABLE align=center bgColor=#006699 border=0 cellPadding=0 cellSpacing=0 
width=770>
  <TBODY>
  <TR bgColor=#006699>
    <TD align=middle bgColor=#006699 id=white><FONT 
    color=#ffffff>对该文的评论</FONT></TD>
    <TD align=middle>
      <SCRIPT 
      src="CSDN_文档中心_微软Office的源代码样式规范 —— 绝对机密文档!!!.files/readnum.htm"></SCRIPT>
    </TD></TR></TBODY></TABLE><BR>
<DIV align=center>
<TABLE align=center bgColor=#cccccc border=0 cellPadding=2 cellSpacing=1 
width=770>
  <TBODY>
  <TR>
    <TH bgColor=#006699 id=white><FONT 
color=#ffffff>我要评论</FONT></TH></TR></TBODY></TABLE></DIV>
<DIV align=center>
<TABLE border=0 width=770>
  <TBODY>
  <TR>
    <TD>你没有登陆,无法发表评论。 请先<A 
      href="http://www.csdn.net/member/login.asp?from=/Develop/read_article.asp?id=3412">登陆</A> 
      <A 
href="http://www.csdn.net/expert/zc.asp">我要注册</A><BR></TD></TR></TBODY></TABLE></DIV><BR>
<HR noShade SIZE=1 width=770>

<TABLE border=0 cellPadding=0 cellSpacing=0 width=500>
  <TBODY>
  <TR align=middle>
    <TD height=10 vAlign=bottom><A 
      href="http://www.csdn.net/intro/intro.asp?id=2">网站简介</A> - <A 
      href="http://www.csdn.net/intro/intro.asp?id=5">广告服务</A> - <A 
      href="http://www.csdn.net/map/map.shtm">网站地图</A> - <A 
      href="http://www.csdn.net/help/help.asp">帮助信息</A> - <A 
      href="http://www.csdn.net/intro/intro.asp?id=2">联系方式</A> - <A 
      href="http://www.csdn.net/english">English</A> </TD>
    <TD align=middle rowSpan=3><A 
      href="http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001032100010"><IMG 
      border=0 height=48 
      src="CSDN_文档中心_微软Office的源代码样式规范 —— 绝对机密文档!!!.files/biaoshi.gif" 
      width=40></A></TD></TR>
  <TR align=middle>
    <TD vAlign=top>百联美达美公司 版权所有 京ICP证020026号</TD></TR>
  <TR align=middle>
    <TD vAlign=top><FONT face=Verdana>Copyright &copy; CSDN.net, Inc. All rights 
      reserved</FONT></TD></TR>
  <TR>
    <TD height=15></TD>
    <TD></TD></TR></TBODY></TABLE></DIV>
<DIV></DIV><!--内容结束//--><!--结束//--></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -