Dynamic polymorphism - explanation

Jarrett Billingsley kb3ctd2 at yahoo.com
Fri Apr 13 05:34:38 PDT 2007


"macky" <martin.butina at gmail.com> wrote in message 
news:evnbo3$30r2$1 at digitalmars.com...
>
> 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

Ah, I see now.  Depending on what you're trying to do, tupleof might be 
unnecessary.  You could design it the other way around, like so:

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

    // To be overridden
    void processMe(Process p);
}

class Customer() : BaseClass
{
   ...some members and overriden baseclass functions...
    void processMe(Process p)
    {
        // do the processing
    }
}

class Process()
{
    void DoSomething(BaseClass object)
    {
        // this will call the appropriate process method based
        // on the derived class type
        object.process(this);
    }
}

But that you want to access data fields of classes and not just the methods 
makes me think you want to do something more complex, like .. serializing 
the members of a class to a file?  I'm wondering why you want to access to 
the data fields of the class. 




More information about the Digitalmars-d-learn mailing list