How do I get a pointer to a method in a class?

Issues and Discussions related to Harbour

Moderator: Rathinagiri

Post Reply
huiyi_ch
Posts: 172
Joined: Sat May 21, 2016 5:27 am

How do I get a pointer to a method in a class?

Post by huiyi_ch »

Hello friends

How do I get a pointer to a method in a class?
For a function, we can return it with the following statement:
point:=@<FunctionName>()
But how do you get a pointer to a method in a class?
Last edited by huiyi_ch on Fri Jun 18, 2021 12:46 pm, edited 1 time in total.
User avatar
gfilatov
Posts: 1060
Joined: Fri Aug 01, 2008 5:42 am
Location: Ukraine
Contact:

Re: How do I get a pointer to a method in a class?

Post by gfilatov »

huiyi_ch wrote: Fri Jun 18, 2021 8:49 am How do I get a pointer to a method in a class?
Hello,

Did you tried the function __objModMethod( oObject, cSymbol, nFuncPtr ) :?:

Please take a look for the working example below:

Code: Select all

#include "hbclass.ch"

Procedure Main()
Local oClass1, oClass2

 oClass1 := Class1()
 oClass2 := Class2()
 __ObjAddMethod(oClass1, "Two",@Two())
 __ObjAddMethod(oClass2, "Two", @Two())
 
 oClass1:Two()
 oClass2:Two()

 __objModMethod(oClass2, "Two", @Three())
 oClass2:Two()

Return

CLASS Class1
 METHOD One BLOCK {|Self| qout("This is Method One of "+::ClassName()) }
END CLASS

CLASS Class2
 METHOD One BLOCK {|Self| qout("This is Method One of "+::ClassName()) }
END CLASS

Function Two()
Local Self := QSelf()

 qout("This is Function Two of "+::ClassName() )
Return ::One()

Function Three()
Local Self := QSelf()

 qout("This is Function Three of "+::ClassName() )
Return Nil
Hope that helps :idea:
Kind Regards,
Grigory Filatov

"Everything should be made as simple as possible, but no simpler." Albert Einstein
huiyi_ch
Posts: 172
Joined: Sat May 21, 2016 5:27 am

Re: How do I get a pointer to a method in a class?

Post by huiyi_ch »

Thank you for answer.
Post Reply