1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
   | class IFirstInterface
{
public:
  virtual int A() = 0;
  virtual int B() = 0;
};
 
class ISecondInterface
{
public:
  virtual int C() = 0;
  virtual int D() = 0;
};
 
class CFirstImpl: public IFirstInterface
{
public:
  int A() {return -1;}
};
 
class CFirstImplPhase2: public CFirstImpl
{
public:
  int B() {return -22;}
};
 
class CSecondImpl: public CFirstImplPhase2, public ISecondInterface
{
public:
  int C() {return -333;}
  int D() {return -4444;}
};
 
class TFirstImpl: public TObject, public IFirstInterface
{
public:
  int A() {return -1;}
};
 
class TFirstImplPhase2: public TFirstImpl
{
public:
  int B() {return -22;}
};
 
class TSecondImpl: public TFirstImplPhase2, public ISecondInterface
{
public:
  int C() {return -333;}
  int D() {return -4444;}
};
 
class TSecondBisImpl: public IFirstInterface, public ISecondInterface
{
public:
  int A() {return -1;}
  int B() {return -22;}
  int C() {return -333;}
  int D() {return -4444;}
};
 
 
 
//---------------------------------------------------------------------------
void __fastcall TLanguageBasicsForm::BtnInterfaceSeparateImplementsClick(TObject *Sender)
{
  IFirstInterface* ObjC = new CFirstImplPhase2();
  OutputDebugString(Format("A : %d - B : %d", ARRAYOFCONST((ObjC->A(), ObjC->B()))).c_str());
  delete ObjC;
 
  IFirstInterface* ObjVCL = new TFirstImplPhase2();
  OutputDebugString(Format("A : %d", ARRAYOFCONST((ObjVCL->A()))).c_str());
  // B() plantera : Pure virtual function called
  // Le programme sera probablement totalement dans les choux (CTRL + F2 pour quitter)
  // OutputDebugString(Format("B : %d", ARRAYOFCONST((ObjVCL->B()))).c_str());
  delete (TFirstImplPhase2*)ObjVCL;
 
  CSecondImpl* ObjC2 = new CSecondImpl();
  IFirstInterface* IC = ObjC2;
  ISecondInterface* IC2 = ObjC2;
  OutputDebugString(Format("A : %d - B : %d - C : %d - D : %d", ARRAYOFCONST((IC->A(), IC->B(), IC2->C(), IC2->D()))).c_str());
  delete ObjC2;
 
  TSecondImpl* ObjVCL2 = new TSecondImpl();
  IFirstInterface* IVCL = ObjVCL2;
  ISecondInterface* IVCL2 = ObjVCL2;
  OutputDebugString(Format("A : %d", ARRAYOFCONST((IVCL->A()))).c_str());
  // OutputDebugString(Format("B : %d", ARRAYOFCONST((IVCL->B()))).c_str());
  OutputDebugString(Format("C : %d", ARRAYOFCONST((IVCL2->C()))).c_str());
  OutputDebugString(Format("D : %d", ARRAYOFCONST((IVCL2->D()))).c_str());
  delete ObjVCL2;
 
  TSecondBisImpl* ObjBis = new TSecondBisImpl();
  IFirstInterface* IBis = ObjBis;
  ISecondInterface* IBis2 = ObjBis;
  OutputDebugString(Format("A : %d - B : %d - C : %d - D : %d", ARRAYOFCONST((IBis->A(), IBis->B(), IBis2->C(), IBis2->D()))).c_str());
  delete ObjBis;
 
}
//--------------------------------------------------------------------------- | 
Partager