How to write something like an interface ?

General help with the eC language.
Post Reply
nicktick
Posts: 17
Joined: Tue Nov 13, 2012 8:34 pm

How to write something like an interface ?

Post by nicktick »

Code: Select all

import "ecere"
class ITest
{
public:
  uint x;
  int  Read(int i);
  void Write();
}

class ITest2 : ITest
{
   uint x;
   int Read(int i)
   {
      return i;
   }
   void Write()
   {
   }
}

class M
{
   bool Test(ITest it)
   {
      it.Read(10);
   }
   int main(int argc,char **argv)
   {
      ITest2 it2{};
      Test(it2);
      return(0);
   }
}
On compiling the above code, the compiler tells:
Linking...
Linker Error: obj/debug.win32/Itest.o: In function `__ecereMethod_M_Test':
Itest.ec:31: error: undefined reference to `__ecereMethod_ITest_Read'
collect2.exe: error: ld returned 1 exit status
jerome
Site Admin
Posts: 603
Joined: Sat Jan 16, 2010 11:16 pm

Re: How to write something like an interface ?

Post by jerome »

Hi nicktick!

eC does not have proper interface support yet (see http://www.ecere.com/mantis/view.php?id=514 for the wishlist feature)

To see what can be done in the current state of things, please take a look at this thread.

To be able to implement multiple interfaces from a single class, interface classes should not contain any data member, and all methods should be virtual and not require a this object, e.g.:

Code: Select all

class ITest
{
public:
  virtual int  ::Read(int i);
  virtual void ::Write();
}
In your case, you were getting this linking error because you declare a non-virtual method without defining it ( no { } body ). The compiler should have reported a compiler error (it's a bug that it did not -- http://www.ecere.com/mantis/view.php?id=1054).
Post Reply