Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2008-01

Multiple inheritance is more powerful than Pascal's interfaces ?

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Andrey Gusev

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.

Reply by email

Matt Emson
to Andrey Gusev
Andrey Gusev wrote 18 lines
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;
Show quoted text 6 lines
class TObj2: TObj { protected: int fff2; };
type
  TObj2 = class(TObj)
  protected
      fff2: integer;
   end;
Show quoted text 5 lines
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;
Show quoted text 5 lines
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.

Reply by email

Andrey Gusev
to Matt Emson

* Matt Emson [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;

Reply by email