Dynamic polymorphism

macky martin.butina at gmail.com
Thu Apr 12 02:06:11 PDT 2007


Hi all!

I'm trying to figure out how dynamic polymorhism is done the right way in D...

interface I
{
    ...some required functions...
}

class BaseClass()
{
    ...implementation of interface...
}

class Customer() : BaseClass
{
   ...some members and overriden baseclass functions...
}

class Process()
{
    void DoSomething(BaseClass object)
    {
        //I want to process all classes derived from base class
    }
}

void Main()
{
   Customer customer = new Customer();
   Process process = new Process();
   process.DoSomething(customer);
}

Even though this is working tupleof is not returning any value (I suppose becouse when creating an instance customer is casted in base class and the link is broken - actualy we are dealing with a partial clone).
Is this the right way of doing things? I know that in c++ you should provide a reference of the customer object not the copy and in c#/java everything is provided by reference. I tried to provide a "ref" in the input params ( void DoSomething(ref BaseClass object)) but this is not accepted. How can I solve this? Thanx.

br, Martin


More information about the Digitalmars-d-learn mailing list