From: Vadim Zeitlin Date: Mon, 18 Oct 1999 13:17:43 +0000 (+0000) Subject: added a rule about DoXXX() functions X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/ae8b97cf4ba6dc8c6bca5e76744c6dccb1ebbb59?ds=inline added a rule about DoXXX() functions git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@4046 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/docs/html/standard.htm b/docs/html/standard.htm index fa5270c36e..15ee6ef106 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

  • @@ -76,10 +76,8 @@ C++ portability guide by David Williams.
  • 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
    @@ -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