Hijacking

eao197 eao197 at intervale.ru
Mon Aug 6 01:17:57 PDT 2007


On Mon, 06 Aug 2007 03:11:14 +0400, Walter Bright  
<newshound1 at digitalmars.com> wrote:

> kris wrote:
>> There's a related problem where a public method is added to a  
>> base-class A (as in your example) but where the signature is *exactly*  
>> that of one existing in derived class B. If A actually calls that new  
>> method internally, "bad things"tm will almost certainly happen, since B  
>> never intended to effectively override the newly-added method in A.
>>  This is a very hard problem to isolate yet can be easily remedied by  
>> the compiler. The request was first made two or three years back, and  
>> once or twice since then: you make the "override" keyword *required*.
>>  When "override" is required, the compiler can easily trap this related  
>> type of hijacking and avoid such nasty surprises.
>
> That is a good point. The reason I haven't added it is because I'm not  
> sure how annoying it will be to have to always add the 'override'  
> keyword. It might be one of those things like exception specifications  
> where everyone says it's a good idea but guiltily hate in secret <g>.

Eiffel has same feature for more than 20 years and it really works :)
Each method which derived class wants to redefine must be declared in  
special section:

class
	BASE
feature
	a is ... end
	b is ... end
	c is ... end
end

class
	DERIVED
inherit
	BASE
		redefine
			a, b
		end
feature
	a is ... end
	b is ... end
end

It is error if 'c' is defined in DERIVED without specifying in 'redefine'  
section.

In addition, Scala requires to use 'override' keyword and it also works  
fine.

-- 
Regards,
Yauheni Akhotnikau



More information about the Digitalmars-d mailing list