Avoiding abstract
Oliver Ruebenkoenig
oliver.ruebenkoenig at web.de
Wed May 30 12:01:59 PDT 2007
Jarrett Billingsley Wrote:
> "Oliver Ruebenkoenig" <oliver.ruebenkoenig at web.de> wrote in message
> news:f3kdri$ap4$1 at digitalmars.com...
> > Hi everyone,
> >
> > the following code does what i want. I was wondering if the same out put
> > can be produced without having the abstract char [] rawSymName(); in the
> > Expr class? Thanks for any hints and thoughts,
> >
> > Oliver
>
> Why do you want to avoid abstract?
If i derive another class, say Integer i'd have to insert something like abstract int val(); for the value of the Integer. Now, I do not want specify an Integer for my Symbol and like wise i do not need a char [] in my Integer. How can i do this? The only thing the expressions share is their virtual head.
the following code does NOT work but shows the problem - i hope ;-)
thanks for the comments concerning the public: and private:
Oliver
import std.stdio;
Expr mySymbol;
Expr myInteger;
public class Expr {
public:
abstract char [] rawSymName();
abstract int val();
abstract Expr head();
}
class Symbol : public Expr {
public:
this( char [] name ) { this.itsName = name; }
Expr head() { return mySymbol; }
char [] rawSymName() { return this.itsName; }
private:
char [] itsName;
}
class Integer : public Expr {
public:
this( int value ) { this.itsValue = value; }
Expr head() { return myInteger; }
int val() { return this.itsValue; }
private:
int itsValue;
}
int main( char [][] arg ) {
Expr myString;
Expr myInt;
mySymbol = new Symbol("Symbol");
myString = new Symbol("String");
myInteger = new Symbol("Integer");
anInt = new Integer(42);
writefln(mySymbol.rawSymName() );
writefln(mySymbol.head().rawSymName() );
writefln(mySymbol.head().head().rawSymName() );
writefln(myString.head().head().rawSymName() );
writefln(myInteger.head().rawSymName() );
// writefln(anInt.val() );
return 0;
}
More information about the Digitalmars-d-learn
mailing list