Recent

Author Topic: (Solved) -Inheritance in free pascal - not getting it (aka being thick :-)  (Read 2707 times)

Dasun

  • New Member
  • *
  • Posts: 41
Note this code entered on a tablet from memory, not copied and pasted..so treat as notional.

So in one unit - say base - I declare -

Class a_base_object = class(TObject)
  Protected
      rt_App_Message
          is_Message : String ;
      End ,

      Function uf_App_Message (art_Message : rt_App_Message ) : Integer ; Virtual; Abstract ;
 
End ;


In another unit ...

Uses base, child

Class Fred = (n_base_Object)
  Type
    uf_App_Message( art_Message : rt_App_Message) : Integer ; Override ;
End ;

Function Fred.uf_App_Message(art_Message : rt_App_Message) : Integer ;
Begin
    //Whatever
End ;

Function test() : Integer ;
Var
   ln_Tom : Tom ;
Begin

   ln_Tom := Tom.Create(nil) ;
   ln_Tom.in_Parent := Self ;
End ;

In another unit - say child

Uses base,

Class Tom = (n_base_object)
    Protected
       in_Parent : n_base_Object ;

End ;

Function test () : Integer ;
    lrt_Message : rt_App_Message
Begin
    ln_Parent.uf_App_message(lrt_App_Message) ; {<<<<<<< Compile Error Identifier idents no member 'uf_app_message'}
End ;


All I am trying to do is add uf_app_message to Tobject - works like I am doing in other languages!  Tried using Helpers- got the same error message, And the less said about Dispatch methods the better - I can not find a single clear example - and yes, I read the example in the doco and I found it as clear as mud.

As always appreciate the help.. 
« Last Edit: October 01, 2016, 08:17:40 pm by Dasun »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Inheritance in free pascal - not getting it (aka being thick :-)
« Reply #1 on: October 01, 2016, 07:01:56 pm »
You added uf_App_Message in a "Protected" section, shouldn't  it be in a "Public" section?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Inheritance in free pascal - not getting it (aka being thick :-)
« Reply #2 on: October 01, 2016, 07:32:34 pm »
Note this code entered on a tablet from memory, not copied and pasted..so treat as notional.
It's hard to guess from broken code (easily spotted syntax errors everywhere), anyone could give false advices due to incomplete information.
Function test () : Integer ;
    lrt_Message : rt_App_Message
Begin
    ln_Parent.uf_App_message(lrt_App_Message) ; {<<<<<<< Compile Error Identifier idents no member 'uf_app_message'}
End ;
ln_Parent is part of Tom class (and is an instance variable, you need Tom instance to access it), while test is a standalone function. So whose In_Parent you're referring to?

Dasun

  • New Member
  • *
  • Posts: 41
Re: Inheritance in free pascal - not getting it (aka being thick :-)
« Reply #3 on: October 01, 2016, 07:47:41 pm »
Thank you for the reply Leledumbo and engkin,

I will give Public a whirl when I get back to my main computer.  yes, code from memory is never a good thing to debug :-) look at a_base_object when I meant n_base_object!  Appreciate you trying!

ln_Parent is declared in the child class and is of type n_base_object . What I was expecting from other languages is that inheriting from an object and using a descendant class would give me access to both the ancestor class and parents methods ...

Hence ln_Parent of class n_base_class would give me access to uf_Message_App  and whatever the ancestor had to offer..

 


Oh .. now why not put ln_Parent in n_base_class - something else to try!

Dasun

  • New Member
  • *
  • Posts: 41
Engkin was on the right track - had to make public .. and put parent in the base class ... Then like most things in FreePascal everything works like a charm first time!!  A nice language to work with, most problems come from newbie misunderstandings, should be used more widely.

Class n_base_object = class(TObject)
  Public
      rt_App_Message
          is_Message : String ;
      End ,

      Function uf_App_Message (art_Message : rt_App_Message ) : Integer ; Virtual; Abstract ;

  Var
    in_Parent : n_base_object
End ;
 
End ;

 

TinyPortal © 2005-2018