View mode: basic / threaded / horizontal-split · Log in · Help
February 04, 2012
Re: what is a usage pattern for "static" in an interface?
On Saturday, February 04, 2012 10:45:05 Zach wrote:
> On Saturday, 4 February 2012 at 05:01:10 UTC, Marco Leise wrote:
> > Am 04.02.2012, 02:54 Uhr, schrieb Martin Nowak
> > 
> > <dawg@dawgfoto.de>:
> >> If override were mandatory in implementation classes we could
> >> easily allow implementations
> >> in interfaces.
> > 
> > Do you have a good example? Mine are currently all solvable
> > with final methods in interfaces, like
> > 
> > 	interface ... {
> > 	
> > 		...
> > 		@property size_t length();
> > 		@property final bool empty() { return length == 0; }
> > 	
> > 	}
> 
> There exists std::list implementations in C++ where length is an
> O(n) operation, but empty could still be O(1).

That's why it's good practice to check whether something is empty rather than 
that its length == 0. Containers can optimize empty when they can't 
necessarily do the same with length. Often it doesn't matter, but in general, 
checking for empty is better because of that.

- Jonathan M Davis
February 05, 2012
Re: what is a usage pattern for "static" in an interface?
On Fri, 03 Feb 2012 02:30:31 -0500, dennis luehring <dl.soluz@gmx.net>  
wrote:

> like:
>
> public interface test
> {
>    public static void blub();
> }
>
> static class test_static: test
> {
>    private static void blub()
>    {
>       int i = 10;
>    }
> }
>
> int main()
> {
>    test_static.blub();
>
>    return 0;
> }
>
> any idea why static could makes sense in an interface? any example?
>
> another thing:
>
> why can i public "blub" in the interface and private it in the class?

static methods are not polymorphic.  The only possible reason to include a  
static method in an interface is purely for namespace purposes.  If a  
static method does not contain an implementation in an interface, it  
should not compile, because there will never be an implementation for it  
-- static methods do not go in the vtable.  In other words, static methods  
could be useful inside an interface definition, but they would not be part  
of the "interface" that an implementing class needs to provide.

note that:

interface test
{
  public static int blub(){return 5;}
}

is no different than:

int blub() {return 5;}

interface test
{
}

except for the namespace blub exists in.

I could see a use case for protected static methods in an interface.   
public ones to a lesser degree, private ones should be outlawed.

-Steve
Next ›   Last »
1 2 3
Top | Discussion index | About this forum | D home