Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!microsoft!petergo From: petergo@microsoft.UUCP (Peter GOLDE) Newsgroups: comp.lang.c++ Subject: Why can't non-virtual functions override virtual ones? Message-ID: <58936@microsoft.UUCP> Date: 9 Nov 90 01:04:41 GMT Reply-To: petergo@microsoft.UUCP (Peter GOLDE) Organization: Microsoft Corp., Redmond WA Lines: 50 In the ARM, p. 209, it says: "An overriding function is itself considered virtual. The virtual specificier may be used for an overriding function in the derived class, but such use is redundant." No justification for this restriction/feature is offered. I'm curious as to why this was put in. It seems to me that it would be a very useful feature to be able to override a virtual function with a non-virtual function; this would allow more efficient access when the type of the object was known. This is especially important when inline functions are used. To be specific, I would like to be able to do things like. struct B { virtual int foo() = 0; }; struct D: public B { int foo() { return 2; } }; struct D2: public B { int foo() { return 3;} }; main() { B *pb = new D; D *pd = new D; D2 *pd = new D2; int x; x = b->foo(); // should do virtual call x = d->foo(); // should inline x = d2->foo(); // also should inline } In essence, not putting "virtual" makes the claim that this implemention of the function will never be overridden. There doesn't seem any reason not to allow this claim for for functions which are themselves overriding other functions; this shouldn't make a difference. Given that C++ has the feature of allowing non-virtual functions for efficiency purposes, why are they restricted in this way? --Peter Golde petergo%microsoft@uunet.uu.net