accessing class methods

General help with the eC language.
Post Reply
pber
Posts: 17
Joined: Mon Aug 18, 2014 2:09 am

accessing class methods

Post by pber »

Hi Jerome,
I can read methods of class Test, but not methods of Test1 , using this code.
(In fact I can read them if Test1 stops inheriting from Test)
What I'm missing?
cheers

Code: Select all

...
Class c = class(Test1);
for(method = (Method)c.methods.first; method; method = (Method)((BTNode)method).next)
   ...

Code: Select all

class Test {
   virtual void run(){}
   virtual void setUp(){}
   virtual void tearDown(){}
   Test(){
      setUp();
   }
   ~Test(){
      tearDown();
   }
};

class Test1 : Test {
   virtual void setUp(){
      PrintLn("t2<setup>");
   }
   virtual void tearDown(){
      PrintLn("t2<release>");
   }
   void testOne(){
      PrintLn("");
   }
};
jerome
Site Admin
Posts: 603
Joined: Sat Jan 16, 2010 11:16 pm

Re: accessing class methods

Post by jerome »

Hi Paolo,

When a base class defines a virtual method, all derived classes will only override that particular method (regardless of whether it is declared as 'virtual' or not in the derived class).

So those virtual methods would only be listed in the base class. You can use c.base to access the base class for that method. The values for the overridden methods themselves would be in the derived class 'vTbl' member.

With virtual methods, invoking the method from an instance of either the base or derived class will result in the derived class method being called.

Please let me know if you need any further clarifications I will be happy to answer in detail.

Best regards,

-Jerome
pber
Posts: 17
Joined: Mon Aug 18, 2014 2:09 am

Re: accessing class methods

Post by pber »

Hi Jerome,
thanks, but I'm confused, so I must backtrack.

Hi Jerome,
in eC: can I get informations about class methods?

i.e.: given

Code: Select all

class Foo : Bar {
  bool doit(){ return true; }
}
Can I obtain a Method instance from the object class(Foo)?
If yes it means I can do the same through a Foo instance: foo.class?
jerome
Site Admin
Posts: 603
Joined: Sat Jan 16, 2010 11:16 pm

Re: accessing class methods

Post by jerome »

Hi Paolo,

Yes you can. But for virtual methods, the Method information only exists in the class where the method is defined, not in the class where it is overridden.
The only thing that the derived method specifies is the function pointer to which it was overridden.
Hence why in your earlier example you if you have:

Class test1Class = class(Test1);

You will want to search for the 'setup' method in the base class of Test1, e.g. test1Class.base (Which will be class(Test) )

For instances, e.g.:

Test1 a { };

You can access the class and then access method from the class:

a._class.base

But instances can also override the virtual method pointers themselves, so for the function pointers you would access a._vTbl[<methodID here>]. The method ID will be the 'vid' within the Method object.

This is all rather internal details though, so depending on what you are trying to achieve there might be an easier way to do it.

Regards,

-Jerome
pber
Posts: 17
Joined: Mon Aug 18, 2014 2:09 am

Re: accessing class methods

Post by pber »

Hi Jerome,

Now I see.
I forgot to explicitly define methods as public.
Then something else joked me: virtual methods are always visible
(where visible means reachable via class.methods tree)
jerome
Site Admin
Posts: 603
Joined: Sat Jan 16, 2010 11:16 pm

Re: accessing class methods

Post by jerome »

Hi Paolo,

Yes, that's correct.
Sorry I omitted those details!

-Jerome
Post Reply