[comp.lang.c++] >>>>>> overriding virtual function...

ssimmons@convex.com (Steve Simmons) (11/10/90)

>>>>>>>>>>>>>>>>>
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
}
>>>>>>>>>>>>>>>



	The reason that you cannot demote a virtual member function
	to a static member function in a derived class lies mainly
	in the implementation.  Each derived class concatenates new 
	fields to the record.  The only way to suppress a field in 
	a derived class is to hide its visibility using an override.
	That is, you can only override by declaring a member of the same 
	name in the derived class.

	In C++, visibility and scoping are done during compile time
	only.  The run time system does not understand any rules of 
	visibility.  It binds the run time 
	call by looking at the field in the instance for the address 
	of the routine.  It does not understand the current class
	when accessing the field.  More runtime checking would be 
	required for it to understand the class.  Thus, slowing down 
	the overall system.   

	If you wish to save on several levels of indirection during the call
	for a virtual function member, you can use the following 
	qualified call, page 210 of the ARM...

		d->D::foo()

	However, it could neither be inlined nor be considered clean code.  

	Thank you. 


					Steve Simmons