Avoiding abstract

Oliver Rübenkönig oliver.ruebenkoenig at web.de
Fri Jun 1 03:46:05 PDT 2007


Thanks for all the comments. They helped me a  great deal to better understand what i wanted to do. I put the working code below.

I am just playing around with data structures. Using a head/tag of the datum is also experimental.

Thanks again,
Oliver


-------------

import std.stdio;

interface Expr {
    Expr head();
    void print();
}

Expr steSymbol;

class Symbol : Expr { 
public:
    this(char [] name) { this.itsName = name; }
    char [] getName() { return this.itsName; }
    Expr head() {return steSymbol;}
    void print() {writefln(this.itsName);}
private:
    char [] itsName;    
}

int main ( char [][] arg ) { 
    Expr e;
    steSymbol = new Symbol("Symbol");
    e = new Symbol("x");
    steSymbol.print();
    e.print();
    e.head().print();
    return 0;
}

Henning Hasemann Wrote:

> 
> If I got you right your problem is: You want to use specific methods of
> derived objects without having to cast them for easy access.
> But you only have a base-class reference available because the method
> that returns the "head" (whatever that means in your case) is available
> in all classes and declared in your base class.
> 
> I would try this:
> 
> * Implement head() in your base class, throwing an exception
> * Derive head but return exactly the type you want to return
> 
> If that doesnt work, because you dont know what head() will return
> for each class in advance, let them all return Expr (as you do now)
> and implement your rawSymName() etc... methods in the base class
> throwing an exception by default.
> Now you only need to override them where you actually need them.
> 
> Only problem with this is, that the virtual dispatch is done at
> runtime, so you can theoretically never be sure if you will get
> a runtime error somewhen because of a typo or something.
> 
> Henning
> 
> -- 
> GPG Public Key: http://keyserver.veridis.com:11371/search?q=0x41911851
> Fingerprint: 344F 4072 F038 BB9E B35D  E6AB DDD6 D36D 4191 1851



More information about the Digitalmars-d-learn mailing list