Multiple inheritance is more powerful than Pascal's interfaces ?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Multiple inheritance is more powerful than Pascal's interfaces ?

Andrey Gusev-3
This question was posted to fpc-other, yesterday, but seems wrongful,
in subject appropriation sense to that maillist.
===========
I wish to introduce some additional (and general) functional to an
existing (and foreign) freepascal's unit. I wouldn't to introduce any
my own code to that foreign module, consider to development process,
svn updation...
With C++'s multiple inheritance quality it is easy to implement:
---
#include <stdio.h>
#include <list>

using namespace std;

class TObj {
protected:
        int fff;
};

class TObj2: TObj {
protected:
        int fff2;
};

class TIntf {
public:
        virtual void ppp() = 0;
};

class TObji: public TIntf,TObj {
public:
        virtual void ppp();
};

class TObj2i: public TObji,TObj2 {
public:
        virtual void ppp();
};

void TObji::ppp()
{
        printf("111 %p\n",this);
        fff = 111;
}

void TObj2i::ppp()
{
        printf("222 %p\n",this);
        fff2 = 222;
        TObji::ppp(); // like "inherited" in pascal
}

typedef list<TIntf*> myList;

int main(void)
{
        myList l;
        l.push_back(new TObji);
        l.push_back(new TObj2i);
        for (myList::iterator i = l.begin(); i != l.end(); ++i)
                (*i)->ppp(); // general interface used
}
---
TObj,TObj2 - the basic classes from foreign module
TObji,TObj2i - my additional functional implementation

But i can't invent nothing like that,
in object pascal, with him interfaces!

Some procedural hack with pointers typecasting already implemented
and works acceptable, it appear to pascal's modular quality.

Can somebody direct me to some object-style variant, maybe objfpc
relieve here (now i use delphi mode) ?

My trial object-style construction is in attachement.
It causes in me some aesthetic dissatisfaction.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

xInjectInterface.pas (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Multiple inheritance is more powerful than Pascal's interfaces ?

Matt Emson
Andrey Gusev wrote:

> This question was posted to fpc-other, yesterday, but seems wrongful,
> in subject appropriation sense to that maillist.
> ===========
> I wish to introduce some additional (and general) functional to an
> existing (and foreign) freepascal's unit. I wouldn't to introduce any
> my own code to that foreign module, consider to development process,
> svn updation...
> With C++'s multiple inheritance quality it is easy to implement:
> ---
> #include <stdio.h>
> #include <list>
>
> using namespace std;
>
> class TObj {
> protected:
>        int fff;
> };

type
   TObj = class
    protected
        fff: integer;
    end;
>
> class TObj2: TObj {
> protected:
>        int fff2;
> };
>

type
  TObj2 = class(TObj)
  protected
      fff2: integer;
   end;

> class TIntf {
> public:
>        virtual void ppp() = 0;
> };
>

  type
     TIntf = interface
        procedure ppp;
     end;

> class TObji: public TIntf,TObj {
> public:
>        virtual void ppp();
> };


type
    TObji = class(TObje, TIntf)
    public
        procedure ppp; virtual;
    end;


> class TObj2i: public TObji,TObj2 {
> public:
>        virtual void ppp();
> };
>

type
   TObj2i = class(TObj2, Tintf) //you do not need to use multiple
inheritance here!! TObj2 already inherits from TObj
   public
      procedure ppp; virtual;
  end;



> But i can't invent nothing like that,
> in object pascal, with him interfaces!
>

That is a translation of what you use as an example.

Alternate route is to use composition and/or aggregation.


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Multiple inheritance is more powerful than Pascal's interfaces ?

Andrey Gusev-3
* Matt Emson <[hidden email]> [Fri, 25 Jan 2008 10:03:59
+0000]:
Matt Emson translation,
that is almost the same as in my
attached trial:
---
   TObj = class
    protected
        fff: integer;
    end;

   TObj2 = class(TObj)
   protected
      fff2: integer;
   end;

   TIntf = interface
      procedure ppp;
   end;

    TObji = class(TObj, TIntf)
    public
       procedure ppp; virtual;
         // AG: virtual only for TObji's
           // descendants will useful
    end;

    TObj2i = class(TObj2, Tintf)
        //you do not need to use multiple
          // inheritance here!! TObj2 already
          // inherits from TObj
        // AG: but TObj2i nowise not bound
          // to TObji, whereas it needed to
          // TObji.ppp be "inherited"
    public
       procedure ppp; virtual;
    end;
---

Actually that looks problematic to me:
---
procedure TObj2i.ppp2;
begin
        TObji(TObj(Self)).ppp2;
        // that looks too arbitrary,
        // have object pascal the
          // same natural solution as
          // C++'s TObji::ppp() call
end;
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal