Classes (does this make sense?)

Saaa empty at needmail.com
Sat May 31 09:03:20 PDT 2008


Let me ask some more specific questions ..

in the previous thread people suggested using the following:

static this()
{
     APPLE = new class() IFruit {
         void eat() {
             writefln("Eat APPLE");
         }
     };

     PEAR = new class() IFruit {
         void eat() {
             writefln("Eat PEAR");
         }
     };
}

Why the 'new class()' ? Couldn't you just create a class apple : IFruit ?
Those fruits are in reality quite big, with multiple functions/local 
variables.
I've tried placing the APPLE in another module:
-----------------------------
module apple;

import std.stdio;
import main;

class APPLE : IFruit {
   void eat() {
      writefln("Eat APPLE");
   }
}
------------------------------
module main;

import std.stdio;
import apple;

interface IFruit
{
     void eat();
}

IFruit APPLE;
IFruit PEAR;
IFruit PLUM;

static this()
{
     PEAR = new class() IFruit {
         void eat() {
             writefln("Eat PEAR");
         }
     };

     PLUM = new class() IFruit {
         void eat() {
             writefln("Eat PLUM");
         }
     };
}

int main(string[] args) {

IFruit[] fruits = [APPLE, PEAR, APPLE, APPLE, PEAR, PLUM];

writefln("Fruits");
foreach( f; fruits) {
f.eat();
}

return 0;
}
---------------------------------

Fruits
Error: Access Violation

:) 




More information about the Digitalmars-d-learn mailing list