X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b4fe51258d17c1e63c55b2b83c8db83e44b15927..e4a71fc3f98e321855aa511e5d84ac08d0b7210a:/docs/html/standard.htm diff --git a/docs/html/standard.htm b/docs/html/standard.htm index fa5270c36e..6cc90a9062 100644 --- a/docs/html/standard.htm +++ b/docs/html/standard.htm @@ -59,8 +59,8 @@ C++ portability guide by David Williams.
  • Turn on all warnings and eradicate them
  • Don't rely on sizeof(int) == 2...
  • No assignments in conditional expressions
  • -
  • Use #if 0 rather than comments to temporarily - disable blocks of code
  • +
  • Use #if 0 rather than comments to temporarily disable blocks of code
  • +
  • Avoid overloaded virtual functions
  • Don't use extra semi-colons on top level

  • @@ -71,15 +71,14 @@ C++ portability guide by David Williams.
  • Avoid carriage returns in cross-platform code
  • Use only lower letter filenames
  • Terminate the files with a new-line
  • +
  • Avoid globals differing by case only

  • Style choices
    1. Naming conventions: use m_ for members
    2. -
    3. Don't use void for functions without - arguments
    4. -
    5. Don't use const for non pointer/reference - arguments
    6. +
    7. Don't use void for functions without arguments
    8. +
    9. Don't use const for non pointer/reference arguments
    @@ -106,8 +105,7 @@ C++ portability guide by David Williams.
  • More about naming conventions
    1. Use wx or WX prefix for all public symbols
    2. -
    3. Use WXDLLEXPORT with all classes/functions in - wxMSW/common code
    4. +
    5. Use WXDLLEXPORT with all classes/functions in wxMSW/common code
    6. Use Set/Get prefixes for accessors
    7. wxNAMING_CONSTANTS
    @@ -464,6 +462,70 @@ instead of The reason is simple: if there are any /* ... */ comments inside ... the second version will, of course, miserably fail. +

  • Avoid overloaded virtual functions
  • + +You should avoid having overloaded virtual methods in a base class because if +any of them is overriden in a derived class, then all others must be overriden +as well or it would be impossible to call them on an object of derived class. + +For example, the following code: + +

    +    class Base
    +    {
    +    public:
    +        virtual void Read(wxFile& file);
    +        virtual void Read(const wxString& filename);
    +    };
    +
    +    class Derived : public Base
    +    {
    +    public:
    +        virtual void Read(wxFile& file) { ... }
    +    };
    +
    +    ...
    +
    +    Derived d;
    +    d.Read("some_filename");    // compile error here!
    +
    + +will fail to compile because the base class function taking filename +is hidden by the virtual function overriden in the derived class (this is +known as [virtual] function name hiding problem in C++). + +

    +The standard solution to this problem in wxWindows (where we have such +situations quite often) is to make both Read() functions not virtual +and introduce a single virtual function DoRead(). Usually, it makes +sense because the function taking a filename is (again, usually) implemented +in terms of the function reading from a file anyhow (but making only this +functions not virtual won't solve the above problem!). +

    +So, the above declarations should be written as: +

    +    class Base
    +    {
    +    public:
    +        void Read(wxFile& file);
    +        void Read(const wxString& filename);
    +
    +    protected:
    +        virtual void DoRead(wxFile& file);
    +    };
    +
    +    class Derived : public Base
    +    {
    +    protected:
    +        virtual void DoRead(wxFile& file) { ... }
    +    };
    +
    + +This technique is widely used in many of wxWindows classes - for example, +wxWindow has more than a dozen of DoXXX() functions which +allows to have many overloaded versions of commonly used methods such as +SetSize() +

  • Don't use extra semi-colons on top level
  • Some compilers don't pay any attention to extra semicolons on top level, as in

    @@ -515,6 +577,13 @@ While DOS/Windows compilers don't seem to mind, their Unix counterparts don't
     like files without terminating new-line. Such files also give a warning message
     when loaded to vim (the Unix programmer's editor of choice :-)), so please think
     about terminating the last line.
    +
    +    

  • Avoid globals differing by case only
  • +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 . +