X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/a68d00b19257c886d48d2583021f308b6bb3d3f5..f21b2fd89d02837dfd0d195f8ff0f3f5640af017:/docs/html/standard.htm?ds=sidebyside diff --git a/docs/html/standard.htm b/docs/html/standard.htm index 1b8e6cf15d..196b63ff5a 100644 --- a/docs/html/standard.htm +++ b/docs/html/standard.htm @@ -9,10 +9,10 @@ - +
- @@ -50,7 +50,14 @@ C++ portability guide by David Williams.
  • Don't use STL
  • Don't declare variables inside for()
  • Don't use nested classes
  • +
  • Don't use new logical operators keywords
  • + +
    +
  • Other compiler limitations
  • +
    1. Use ternary operator ?: carefully
    2. +
    3. Don't use initializers with automatic arrays
    4. +
    5. Always have at least one constructor in a class with destructor

  • General recommendations
  • @@ -264,30 +271,34 @@ The scope of a variable declared inside for() statement changed several years ago, however many compilers still will complain about second declaration of i in the following code:
    -  for ( int i = 0; i < 10; i++ ) {
    +  for ( int i = 0; i < 10; i++ ) {
         ...
       }
     
       ...
     
    -  for ( int i = 0; i < 10; i++ ) {
    +  for ( int i = 0; i < 10; i++ ) {
         ...
       }
     
    -Even if it's perfectly legal now. +even though if it's perfectly legal now.

    Workaround: write this instead:

       int i;
    -  for ( i = 0; i < 10; i++ ) {
    +  for ( i = 0; i < 10; i++ ) {
         ...
       }
     
       ...
     
    -  for ( i = 0; i < 10; i++ ) {
    +  for ( i = 0; i < 10; i++ ) {
         ...
       }
     
    +or, even better, use different names for the variables in the different for +loops (in particular, avoid mute variable names like i above) - then +you can declare them in the loop statement and don't pollute the outer name +space with local loop variables.

  • Don't use nested classes
  • Nested classes are, without doubt, a very good thing because they allow to hide @@ -333,28 +344,73 @@ you can try the following: including the header if you change the PrivateLibClass declaration (it's an example of a more general interface/implementation separation idea). +

  • Don't use new logical operators keywords
  • +The C++ standard has introduced the following new reserved words: or, +and, not, xor, bitand, bitor, +compl, and_eq, or_eq, not_eq, +or_eq which can be used instead of the usual C operations &&, +||, ~ etc. +

    This wonderful (and not backwards compatible in addition to being +absolutely useless) new feature means that these new keywords should not be +used as the variable names - even if current compilers usually will accept +this, your code will break in the future. For most of the keywords, using them +as variable names is quite unlikely, but or and compl were +used in the wxWindows sources which seems to indicate that they are the most +likely candidates. +

    It goes without saying that these new keywords should not be used instead +of the tradional C operators neither both because most compilers don't accept +them and because using them in C code makes it less readable. + + +
    +

  • Other compiler limitations
  • +This section lists the less obvious limitations of the current C++ compilers +which are less restrictive than the ones mentioned in the previous section but +are may be even more dangerous as a program which compiles perfectly well on +some platform and seems to use only standard C++ featurs may still fail to +compile on another platform and/or with another compiler. + +

    1. Use ternary operator ?: carefully
    2. The ternary operator ?: shouldn't be used with objects (i.e. if any -of its operands are objects) because some compilers (notable Borland C++) fail +of its operands are objects) because some compilers (notably Borland C++) fail to compile such code.

      Workaround: use if/else instead.

           wxString s1, s2;
       
           // Borland C++ won't compile the line below
      -    wxString s = s1.Len() < s2.Len() ? s1 : s2;
      +    wxString s = s1.Len() < s2.Len() ? s1 : s2;
       
           // but any C++ compiler will compile this
           wxString s;
      -    if ( s1.Len() < s2.Len() )
      +    if ( s1.Len() < s2.Len() )
               s = s1;
           else
               s = s2;
       
      + +

    3. Don't use initializers with automatic arrays
    4. +The initializers for automatic array variables are not supported by some older +compilers. For example, the following line +

      +    int daysInMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
      +
      +will fail to compile with HP-UX C++ compiler. +

      Workaround: either make the array static or initialize each item +separately: in the (stupid) example above, the array should be definitely +declared as static const (assuming that the leap years are dealt with +elsewhere somehow...) which is ok. When an array is really not const, you +should initialize each element separately. + +

    5. Always have at least one constructor in a class with destructor
    6. +It is a good rule to follow in general, but some compilers (HP-UX) enforce it. +So even if you are sure that the default constructor for your class is ok but +it has a destructor, remember to add an empty default constructor to it.


    -
  • General recommendations
  • +

  • General recommendations
  • While the recommendations in the previous section may not apply to you if you're only working with perfect compilers which implement the very newest directives of C++ standard, this section contains compiler- (and language-) independent advice @@ -551,7 +607,7 @@ put another one after it.
    -

  • Unix/DOS differences
  • +

  • Unix/DOS differences
  • Two operating systems supported by wxWindows right now are (different flavours of) Unix and Windows 3.1/95/NT (although Mac, OS/2 and other ports exist/are being developed as well). The main differences between them are summarized @@ -595,23 +651,24 @@ about terminating the last line. The linker on VMS is case-insensitive. Therefore all external variables and functions which differ only in case are not recognized by the linker as different, so all externals should differ in more than the case only: -i.e. GetId is the same as . +i.e. GetId is the same as GetID.
    -

  • Style choices
  • +

  • Style choices
  • All wxWindows specific style guidelines are specified in the next section, here are the choices which are not completely arbitrary, but have some deeper and not wxWindows-specific meaning.

    1. Naming conventions: use m_ for members
    2. -It's extremely important to write readable code. One of the first steps in this -direction is the choice of naming convention. It may be quite vague or strictly -define the names of all the variables and function in the program, however it -surely must somehow allow the reader to distinguish between variable and -functions and local variables and member variables from the first glance. +We all know how important it is to write readable code. One of the first steps +in this direction is the choice of naming convention. It may be quite vague or +strictly define the names of all the variables and function in the program, +however it surely must somehow allow the reader to distinguish between +variable and functions and local variables and member variables from the first +glance.

      The first requirement is commonly respected, but for some strange reasons, the second isn't, even if it's much more important because, after all, the immediate context usually allows you to distinguish a variable from a function in @@ -730,8 +787,8 @@ the right header for given platform. Any new headers should conform to this setup as well to allow including <wx/foo.h> on any platform.

      Note that wxWindows implementation files should use quotes when including wxWindows -headers, not angled brackets. Applications should use angled brackets. There -is a reason for it (can anyone remember what this is?). +headers, not angled brackets. Applications should use angled brackets. This +ensures that the dependencies are correctly handled by the compiler.

    3. Include guards

      To minimize the compile time C++ programmers often use so called include

    - + + wxWindows Programmer Style Guide