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.
+ +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() +
Some compilers don't pay any attention to extra semicolons on top level, as in