2012年3月26日星期一

Beats By DreWith the officialtranslation ( )

With the officialtranslation ( ) ,this is my original translation .( namespace ) in the. CC advocated the use of anonymous namespace ( unnamed namespace ) .And the name namespace ( named namespace ) nomenclature to project and (if not in the root directory .
) project in the path name .Do not use the using keyword .Definition :a namespace to scope is divided into independent of each other ,with a specific name scope .This can be avoided in the global scope to produce the same conflict .
Advantages: the class provides ( level ) of the isolation method is provided based on a set of ( a level ) named isolation method .For example ,two different engineering all contain the global category Foo ,the two class name may at compile-time or run-time generated homonymic conflict .
However ,Dre Beats,if in each project are to put them into the corresponding namespace ,project1: :Foo and project2: :Foo is two completely different names ,but will not produce a homonymic conflict .
Disadvantages :namespace is easy to let the human feel confused, because the class itself has provided a set of ( level ) of isolation methods ,and on this basis the namespace added a set of ( level ) of isolation method .
In header files using anonymous namespace ,easy to violate C + + single ( One Definition Rule ) defines the rules .Conclusion :according to the following principles using namespace .Anonymous namespace in the.
CC file, not only allows the use of anonymous namespace ,and ,in order to avoid run-time homonymic conflict ,have advocated the use of anonymous namespace :however ,the document with a particular class of related entities ,should be in the class declared as type ,static or static member function ,but not in the class declared as a member of anonymous namespace .
As this example shows ,in an anonymous space at the end to add annotations &ldquo ;/ / namespace &rdquo ;.In the. H file do not use anonymous namespace .Namespace namespace name name usage is as follows: in addition to the file of the front of the include statement ( statement ) ,define and lead statement of the other class in the namespace ,namespace contains the entire source code file contents :typical .
CC files can be very complex ,and may refer to any other class in the namespace .Not in the STD namespace definition statement ,including the standard library class forward declaration .
In a namespace declaration of any entity is undefined behavior ( undefined behavior ) ,that is to say not portable .Statement of standard library entity method is to include the appropriate header .
There is no need to use the using statement to all entities are available within a namespace .In the. CC file to any location you can use the using statement ,in the. H file function ,method and class can use the using statement .
In the. CC file in any place ,including the overall file naming named entity anywhere within ,as well as the function and method of internal can use named entity alias ( namespace aliases ) .
Attention ,defined in the header file aliases ,in any include this header file is visible .So in the public file ( the header file could be in the works by inclusion) ,Vibram 5 Fingers Sale,and indirectly by these public header file header ,should avoid to use the alias .
It is also maintained by the API as succinct as possible target part. ) Although ,when nested class is part of the interface ,can be declared as public ,but the best is to use a namespace to nested class declarations and global scope isolation .
Definition :in class defined inside another class ;the internal class called members of the class ( member class ) .Advantages :when only the inline class outer class ( enclosing class ) use it very useful, then it can be defined as the members of the class ,thus it is defined in the outer scope of internal class ,avoid the statement in the outer scope brings pollution (i.
e., does not affect the use of other class ) .Nested classes can lead in the outer class statement ,and then in the. CC file definition .In this way ,you can avoid the outer class definition define a nested class ,and nested classes generally only and the implementation of .
Disadvantages :the inline class only in the outer class definition front statement .Therefore ,any use of Foo: :Bar * pointer head file needs to contain Foo class .Conclusion :unless the inline class is part of the interface ,otherwise not declared as public ,for example ,a class contains a method of multiple options .
) Static member function ( Static ,Member ) ,global function ( Global Functions ) preferentially use namespace nonmember functions or static member function ,try not to use global functions .
Advantages :sometimes ,a member function and static member function is very useful .A member function in a namespace ,can prevent the global namespace name pollution .Disadvantages :the member function and static function as a new class member functions may be more reasonable ,especially when these functions to access a large number of external resources or with a large number of dependent .
Conclusion :sometimes ,the definition of a class function is useful ,Five Fingers Shoes,even is a must .Such a function can be a static member function or a member function .Non member function should not rely on external variables ,and should always exists only in a namespace .
Don to excluding shared static data static member function of packaging, the function to the and other classes in the same compilation unit defined function ,if the other compilation units called directly ,may cause additional coupling and connectivity dependent ,a static member function especially apparent .
At this moment ,should consider establishing a new class to encapsulate these functions ,these functions can be encapsulated into separate classes .If you must define a nonmember function ,and only in the current .
CC file in use ,the use of anonymous namespace or static join keywords (such as static int Foo ( ) {... } ) to limit its scope. ) The function of the variable scope to restrict it to a small range, and the definition is initialized .
C + + allows function anywhere in the declaration of variables .However ,in order to minimize the scope declarations of variables ,preferably before first use statement .So ,read code person is very easy to find the definition ,to know that this variable is what type ,initial value is a number .
It should be emphasized that :define and initialize the should be placed in the same sentence ,rather than first defined separately assignment .Notably ,GCC of for ( int i = 0 ;I < ;10 ;+ + I ) statement is correct ( I scope variables defined within a for loop ) .
Therefore ,in this for circulating in the same scope of other for loop can also use the variable I .GCC on if and while statement declared in the scope of variables of the implementation is correct .
For example :warning: if the variable is an object ,then in each entry of scope are calling the constructor and create the object ,while in the field and then calls the destructor to destroy the object .
The variables declared in the circulation from the body to improve efficiency. ) And the global variable ( Global Variable ) banned the use of static or global class type variables :they cause is not easy to find bug ,Moncler Outlet,as they are constructed and the destructor order is not fixed .
If the object is stored in memory in the quiescent zone ( including global variables ,static variables ,a static class member variable and function of internal static variables ) ,must be simple old data ( Plain Old Data ) ,namely integer ,character ,single or simple old data types ,array ,pointer structure .
However ,in C + + does not stipulate the static constructor and destructor calling sequence ,even each compiler generated after the calling sequence may be different .This may lead to not easy to find bug .
Therefore ,not only against type as a global variable ,also prohibit the use function return value on the static simple old data variables are initialized, unless this function ( such as getenv ( ) ,or getpid ( ) ) itself is not dependent on other variables .
According to the regulations,Beats By Dre, the destructor invocation order and the order of constructor calls should be exactly the opposite .Because the order of constructor calls is uncertain ,so the destructor call sequence is uncertain .
For example :in the course of running ,a static variable is destroyed ,but the program is still in operation ,may be just another thread to use the variable ,which of course will fail .Or is in a call to a certain &ldquo ;string &rdquo ;type static destructor ,only to call another variable in the destructor ,this variable contains the string reference .
Thus ,provisions of static variable is only simple old data types .This rule clearly prohibit the vector ( you can use the C language array instead) and string ( you can use the const char ) are declared as static .
If you need to use a static or global class type variables ,then please main function or the pthread_once function initialize a pointer ( this pointer will not be released ) .Note ,this must be a raw pointer ( raw pointer ) ,and cannot be a smart pointer ( smart pointer ) ,as a smart pointer destructor has discussed above the calling sequence problem .
Related articles:

没有评论:

发表评论