Pointer to interface method

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Sun Apr 15 10:37:27 PDT 2007


Mandel wrote:
> Hi,
> 
> I like to have a pointer to an interface method,

What possible use could this have? Interface methods are by definition 
abstract, and are thus never directly defined.

(Note that D doesn't support the concept of a "member pointer" as C++ does)

> like this:
> 
> interface Item
> {
> 	void work();
> }
> 
> int main(char[][] args)
> {
> 	
> 	auto s = &Item.work
> 	return 0;
> }
> 
> But this example fails because of an "undefined symbol" for Item.work.

Actually, it fails with "semicolon expected following auto declaration, 
not 'return'" :).

> Is this a bug or intended behaviour?

What happens here is that the compiler tells the linker to fill in a 
pointer to the relevant method at link time, but in the case of an 
abstract method the symbol simply doesn't exist. There's nothing 
sensible to do here other than produce an error.

The fact that the error isn't produced until link-time instead of at 
compile-time could be considered a bug though, since the compiler can 
determine the function can't possibly be defined in a legal program.


More information about the Digitalmars-d-learn mailing list