TITLE: Making all methods virtual RESPONSE: feustel@netcom.com (David Feustel) Coplien's book "Advanced C++" makes the point that very little overhead would be incurred in most programe even if all functions were declared virtual and that some programs might even run faster than equivalent C programs using switch constructs to achieve the same effects. RESPONSE: brendan@us-es.sel.de (Dalton), 14 Sep 92 This reminds me of a question which I've been meaning to ask for some time now. Ignoring performance issues, why would one not declare every method in a class virtual? What circumstances might preclude its use? RESPONSE: kanze@us-es.sel.de (James Kanze) Classes are not only used for OO stuff in C++, and it's not necessarily reasonably to be able to overload the other stuff. In particular, I tend not to make overloaded operators virtual (in the absence of multiple dispatch). For example, if I define: class B { const B& operator+=( const B& rightHandSide ) ; } ; How do you go about overloading this. Basically, your derived class has to be able to handle *any* class derived from B on the right hand side, ie: class B { virtual const B& operator+=( const B& rightHandSide ) ; } ; class D1 : public B { // Note the types on the following. This is necessary // if overloading is to work. const B& operator+=( const B& rightHandSide ) ; } ; class D2 // like D1 Now what do you do in D1::operator+= if the actual type of the parameter is a D2. (Note that you may not even have known about D2 when you wrote the class.) Because of the ambiguities (in the minds of the user), I tend to make *nothing* virtual in classes that simply implement new data types, like Complex or String. This tends to preclude there use as base classes, especially if, like String, they have destructors. IMHO, such classes really have nothing to do with OOP. Which is not to say they aren't useful. (I use String in almost everything I write.) On the other hand, when my classes derive from an object oriented design, and polymorphism is important, I tend to avoid overloading operators, with the possible exception of assignment and the comparison operators, and even here... How *do* you implement such operators safely in the absence of run time type information?