Results 1 to 6 of 6

Thread: Calling a method from a non-member method

  1. #1
    Join Date
    Mar 2011
    Posts
    23
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Calling a method from a non-member method

    Hi, I need to call a method from a non-member function. Is there a way to reach an object from the outside? (i'm sorry that i can't find the right way to express my question, but please bear with me...) I'm loking for something like
    Appname->object.processMessage()
    but I can't find a way to do it.
    This is a stub of my class implementation.
    I'm working with a subclass of QThread and HLP_Init comes from an external library. I tried to pass the method directly to the HLP Init, but that did not work because the compiler complained that the type of function was not right.

    Qt Code:
    1. void HLPReceiveMessage(tyHLP_Message message, tyHLP_LinkID linkId)
    2. {
    3. //i need to call void HLP_LinkManager::processMessage() here
    4. }
    5.  
    6. void HLPReceiveError(tyHLP_Error err, tyHLP_LinkID linkId)
    7. {
    8. }
    9.  
    10. void HLP_LinkManager::run()
    11. {
    12. int rc;
    13. //HLP server initialization
    14. rc=HLP_Init(HLP_LINK_SERVER,HLPReceiveMessage,HLPReceiveError);
    15. }
    16.  
    17. void HLP_LinkManager::processMessage()
    18. {
    19. //do stuff here
    20. }
    To copy to clipboard, switch view to plain text mode 

    And thanks in advance!
    Last edited by AndresBarbaRoja; 17th March 2011 at 17:31.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Calling a method from a non-member method

    Just add a parameter to the functions, of type HLP_LinkManager*.
    Qt Code:
    1. void HLP_LinkManager::run()
    2. {
    3. int rc;
    4. //HLP server initialization
    5. rc=HLP_Init(HLP_LINK_SERVER,HLPReceiveMessage,HLPReceiveError,this);
    6. }
    7.  
    8.  
    9. void HLPReceiveMessage(tyHLP_Message message, tyHLP_LinkID linkId,HLP_LinkManager* pHlpMgr)
    10. {
    11. pHlpMgr->processMessage();
    12. }
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Mar 2011
    Posts
    23
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Calling a method from a non-member method

    The problem is that I can't alter the HLP_Init nor the RecieveMessage declarations. That is why i'm trying to acess from a global variable.
    In any case, taking into account your idea, i've been thinking in whether i can wrap those functions, but i can't get to anything that works.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,677
    Thanks
    13
    Thanked 1,596 Times in 1,524 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Calling a method from a non-member method

    You need an instance of HLP_LinkManager to be able to call non-static member methods. If you cannot provide an instance then you cannot access these methods.

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Calling a method from a non-member method

    in that case, the only option I see is using a global:
    Qt Code:
    1. HLP_LinkManager *g_pLinkMgr = NULL;
    2.  
    3. void HLPReceiveMessage(tyHLP_Message message, tyHLP_LinkID linkId)
    4. {
    5. //i need to call void HLP_LinkManager::processMessage() here
    6. if(g_pLinkMgr){
    7. g_pLinkMgr->processMessage();
    8. }
    9. }
    10.  
    11. void HLP_LinkManager::run()
    12. {
    13. int rc;
    14. g_pLinkMgr = this;
    15. //HLP server initialization
    16. rc=HLP_Init(HLP_LINK_SERVER,HLPReceiveMessage,HLPReceiveError);
    17. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by high_flyer; 19th March 2011 at 22:58. Reason: typo
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. The following user says thank you to high_flyer for this useful post:

    AndresBarbaRoja (19th March 2011)

  7. #6
    Join Date
    Mar 2011
    Posts
    23
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Talking Re: Calling a method from a non-member method

    Thanks, the code that you provided is better than mine, i did not thought about leaving the global as null and then using "this", but now this way solves my problem

Similar Threads

  1. Calling a method to a parent window/widget
    By johnnyturbo3 in forum Newbie
    Replies: 4
    Last Post: 6th November 2010, 16:52
  2. error calling method using connect
    By jeffmetal in forum Newbie
    Replies: 4
    Last Post: 22nd April 2010, 19:09
  3. Replies: 1
    Last Post: 30th March 2009, 17:07
  4. Calling same method from separate threads
    By steg90 in forum Qt Programming
    Replies: 2
    Last Post: 19th July 2007, 09:55
  5. Replies: 4
    Last Post: 10th March 2007, 19:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.