Dynamic polymorphism - explanation

macky martin.butina at gmail.com
Fri Apr 13 00:33:23 PDT 2007


Jarrett Billingsley Wrote:

> "macky" <martin.butina at gmail.com> wrote in message 
> news:evksq3$223e$1 at digitalmars.com...
> > 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.
> 
> What _are_ you doing, anyway?  Why do you need to use .tupleof to do 
> polymorphism, why are all your classes templates with empty parameter lists, 
> and what does "I want to process all classes derived from base class" mean? 
> 
> 

ah. sorry. Classes are empty due to my lazyness ;). Actualy there are some virtual methods defined in a base class but some members are are defined just in a derived class. My idea was that I would get this members with tuppleof function (I guess I'm trying to fake the reflection). These members do not exist in a base class ofcourse but my question is if this is possible the way I wanted to perform this task... Since my DoSomething() function is expecting the base class and in implementation I have provided the derived class I would expect that it is handled as a derived class. But I realize that this derived class is cast in a base class and therefor no member is returned. I would like to avoid this but I don't know how. I thought that I'm not using input parameters correctly?

regards, Martin


More information about the Digitalmars-d-learn mailing list